<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Chloneda</title>
    <link>https://chloneda.github.io/</link>
    <atom:link href="/rss2.html" rel="self" type="application/rss+xml"/>
    
    <description>Less is more</description>
    <pubDate>Tue, 31 Mar 2020 01:49:08 GMT</pubDate>
    <generator>http://hexo.io/</generator>
    
    <item>
      <title>git config全局和本地配置</title>
      <link>https://chloneda.github.io//blog/git-config/</link>
      <guid>https://chloneda.github.io//blog/git-config/</guid>
      <pubDate>Tue, 31 Mar 2020 01:49:08 GMT</pubDate>
      <description>
      
        
        
          &lt;p&gt;&lt;strong&gt;注：本文出自博主 Chloneda&lt;/strong&gt;：&lt;a href=&quot;https://chloneda.github.io/&quot;&gt;个人博客&lt;/a&gt; | &lt;a href=&quot;https://www.cnblogs.com/chloneda&quot; target=&quot;_b
        
      
      </description>
      
      <content:encoded><![CDATA[<p><strong>注：本文出自博主 Chloneda</strong>：<a href="https://chloneda.github.io/">个人博客</a> | <a href="https://www.cnblogs.com/chloneda" target="_blank" rel="noopener">博客园</a> | <a href="https://github.com/chloneda" target="_blank" rel="noopener">Github</a> | <a href="https://gitee.com/chloneda" target="_blank" rel="noopener">Gitee</a> | <a href="https://www.zhihu.com/people/chl_vip/" target="_blank" rel="noopener">知乎</a></p><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>程序开发人员在日常开发过程中，使用 <strong>git</strong> 可能会遇到这样一个情况：</p><ul><li>公司项目使用公司账号提交git信息。</li><li>个人业余项目使用个人账户提交git信息。</li></ul><p>也就是说，我们需要 <strong>git</strong> 根据不同项目使用不同的账号及邮箱提交相关记录，本文就是专门解决这个问题的。</p><h1 id="本地设置"><a href="#本地设置" class="headerlink" title="本地设置"></a>本地设置</h1><p>本地设置有两种方式，<strong>命令方式</strong> 和 <strong>配置文件方式</strong>，两种方式选择任意一种，都可以配置当前git项目提交git信息的账号及邮箱。</p><h2 id="命令方式"><a href="#命令方式" class="headerlink" title="命令方式"></a>命令方式</h2><p>对于本地 <strong>git repo</strong> 配置，先进入 <strong>本地git repo</strong> 目录，使用命令：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config user.name <span class="string">"your-username"</span></span><br><span class="line">git config user.email <span class="string">"your-email-address"</span></span><br></pre></td></tr></table></figure><h2 id="配置文件方式"><a href="#配置文件方式" class="headerlink" title="配置文件方式"></a>配置文件方式</h2><p>对于本地 <strong>git repo</strong> 配置，先进入 <strong>本地git项目</strong> 目录，编辑 <strong>.git/config</strong> 文件，增加以下信息：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[user]</span><br><span class="line">    name = your-username</span><br><span class="line">    email = your-email-address</span><br></pre></td></tr></table></figure><p>保存并退出。以上 <strong>命令方式</strong> 和 <strong>配置文件方式</strong> 两种方式，都可以在 <strong>本地git repo</strong> 目录下，通过以下命令查看本地项目的账号及邮箱是否更改成功。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config user.name</span><br></pre></td></tr></table></figure><p>对于很多个 <strong>本地git repo</strong> 来说，每个 <strong>本地git repo</strong> 都要设置自定义化git本地账号及邮箱，有时我们不想每个都设置，我们可以使用git的全局配置，所有 <strong>本地git repo</strong> 都是使用全局配置！</p><h1 id="全局设置"><a href="#全局设置" class="headerlink" title="全局设置"></a>全局设置</h1><p>全局设置有两种方式，<strong>命令方式</strong> 和 <strong>配置文件方式</strong>，两种方式选择任意一种，都可以配置全局git项目提交git信息的账号及邮箱。</p><h2 id="命令方式-1"><a href="#命令方式-1" class="headerlink" title="命令方式"></a>命令方式</h2><p>在git服务器中，任意非 <strong>本地git repo</strong> 中，使用以下命令配置全局配置：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name <span class="string">"your-username"</span></span><br><span class="line">git config --global user.email <span class="string">"your-email-address"</span></span><br></pre></td></tr></table></figure><h2 id="配置文件方式-1"><a href="#配置文件方式-1" class="headerlink" title="配置文件方式"></a>配置文件方式</h2><p>编辑 <strong>〜/.gitconfig</strong>，其内容与 <strong>.git/config</strong> 文件中的内容相同，即将 <strong>[user]</strong> 部分信息添加至 <strong>〜/.gitconfig</strong> 文件中，内容如下：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[user]</span><br><span class="line">    name = your-username</span><br><span class="line">    email = your-email-address</span><br></pre></td></tr></table></figure><p>保存并退出。任意非 <strong>本地git repo</strong> 中，在使用以下命令查看是否配置成功</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config user.name</span><br></pre></td></tr></table></figure><p>好了，这次git config全局及本地配置就到这里了！</p><hr>]]></content:encoded>
      
      
    </item>
    
    <item>
      <title>Vim命令速查表</title>
      <link>https://chloneda.github.io//blog/vim-cheatsheet/</link>
      <guid>https://chloneda.github.io//blog/vim-cheatsheet/</guid>
      <pubDate>Mon, 30 Mar 2020 14:33:48 GMT</pubDate>
      <description>
      
        
        
          &lt;p&gt;&lt;strong&gt;注：本文出自博主 Chloneda&lt;/strong&gt;：&lt;a href=&quot;https://chloneda.github.io/&quot;&gt;个人博客&lt;/a&gt; | &lt;a href=&quot;https://www.cnblogs.com/chloneda&quot; target=&quot;_b
        
      
      </description>
      
      <content:encoded><![CDATA[<p><strong>注：本文出自博主 Chloneda</strong>：<a href="https://chloneda.github.io/">个人博客</a> | <a href="https://www.cnblogs.com/chloneda" target="_blank" rel="noopener">博客园</a> | <a href="https://github.com/chloneda" target="_blank" rel="noopener">Github</a> | <a href="https://gitee.com/chloneda" target="_blank" rel="noopener">Gitee</a> | <a href="https://www.zhihu.com/people/chl_vip/" target="_blank" rel="noopener">知乎</a></p><h1 id="Vim命令速查表"><a href="#Vim命令速查表" class="headerlink" title="Vim命令速查表"></a>Vim命令速查表</h1><blockquote><p>个人Vim命令速查表。</p></blockquote><p><a href="https://github.com/chloneda/vim-cheatsheet" target="_blank" rel="noopener">vim-cheatsheet</a> | <a href="https://www.vim.org/" target="_blank" rel="noopener">Vim官网</a> | <a href="https://github.com/vim/vim" target="_blank" rel="noopener">Github</a> | <a href="http://vimcdoc.sourceforge.net/doc/help.html" target="_blank" rel="noopener">Vim中文文档</a></p><ul><li><a href="https://github.com/chloneda/vim-cheatsheet/blob/master/vimrc" target="_blank" rel="noopener">Vim自定义配置文件 - vimrc</a></li><li><a href="https://github.com/chloneda/vim-cheatsheet/blob/master/vimium.md" target="_blank" rel="noopener">Chrome浏览器神级插件 - Vimium</a></li></ul><h2 id="光标移动"><a href="#光标移动" class="headerlink" title="光标移动"></a>光标移动</h2><p><strong>注：一般模式下，任意一个动作都可以重复。</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">h                   <span class="comment"># 光标左移，同 &lt;Left&gt; 键</span></span><br><span class="line">j                   <span class="comment"># 光标下移，同 &lt;Down&gt; 键</span></span><br><span class="line">k                   <span class="comment"># 光标上移，同 &lt;Up&gt; 键</span></span><br><span class="line">l                   <span class="comment"># 光标右移，同 &lt;Right&gt; 键</span></span><br><span class="line">Ctrl+e              <span class="comment"># 向上滚动一行</span></span><br><span class="line">Ctrl+y              <span class="comment"># 向下滚动一行</span></span><br><span class="line">Ctrl+u              <span class="comment"># 向上滚动半屏 --&gt; move up 1/2 a screen</span></span><br><span class="line">Ctrl+d              <span class="comment"># 向下滚动半屏 --&gt; move down 1/2 a screen</span></span><br><span class="line">Ctrl+f              <span class="comment"># 向下滚动一屏 --&gt; move forward one full screen</span></span><br><span class="line">Ctrl+b              <span class="comment"># 向上滚动一屏 --&gt; move back one full screen</span></span><br><span class="line">0                   <span class="comment"># 跳到行首（是数字零，不是字母O），效用等同于 &lt;Home&gt; 键</span></span><br><span class="line">^                   <span class="comment"># 跳到从行首开始第一个非空白字符</span></span><br><span class="line">$                   <span class="comment"># 跳到行尾，效用等同于 &lt;End&gt; 键</span></span><br><span class="line">gg                  <span class="comment"># 跳到第一行，效用等同于 Ctrl+&lt;Home&gt;</span></span><br><span class="line">G                   <span class="comment"># 跳到最后一行，效用等同于 Ctrl+&lt;End&gt;</span></span><br><span class="line">nG                  <span class="comment"># 跳到第n行，比如 10G 是移动到第十行</span></span><br><span class="line">:n                  <span class="comment"># 跳到第n行，比如 :10&lt;回车&gt; 是移动到第十行</span></span><br><span class="line">10%                 <span class="comment"># 移动到文件 10% 处</span></span><br><span class="line">15|                 <span class="comment"># 移动到当前行的 15列</span></span><br><span class="line">w                   <span class="comment"># 跳到下一个单词开头 (word: 标点或空格分隔的单词)</span></span><br><span class="line">W                   <span class="comment"># 跳到下一个单词开头 (WORD: 空格分隔的单词)</span></span><br><span class="line">e                   <span class="comment"># 跳到下一个单词尾部 (word: 标点或空格分隔的单词)</span></span><br><span class="line">E                   <span class="comment"># 跳到下一个单词尾部 (WORD: 空格分隔的单词)</span></span><br><span class="line">b                   <span class="comment"># 跳到上一个单词开头 (word: 标点或空格分隔的单词)</span></span><br><span class="line">B                   <span class="comment"># 跳到上一个单词开头 (WORD: 空格分隔的单词)</span></span><br><span class="line">ge                  <span class="comment"># 跳到前一个单词结尾</span></span><br><span class="line">)                   <span class="comment"># 向前移动一个句子（句号分隔）</span></span><br><span class="line">(                   <span class="comment"># 向后移动一个句子（句号分隔）</span></span><br><span class="line">&#125;                   <span class="comment"># 向前移动一个段落（空行分隔）</span></span><br><span class="line">&#123;                   <span class="comment"># 向后移动一个段落（空行分隔）</span></span><br><span class="line">&lt;enter&gt;             <span class="comment"># 移动到下一行首个非空字符</span></span><br><span class="line">n&lt;enter&gt;            <span class="comment"># 光标向下移动 n 行</span></span><br><span class="line">+                   <span class="comment"># 移动到下一行首个非空字符（同回车键）</span></span><br><span class="line">-                   <span class="comment"># 移动到上一行首个非空字符</span></span><br><span class="line">H                   <span class="comment"># 移动到屏幕上部</span></span><br><span class="line">M                   <span class="comment"># 移动到屏幕中部</span></span><br><span class="line">L                   <span class="comment"># 移动到屏幕下部</span></span><br><span class="line">&lt;S+Left&gt;            <span class="comment"># 按住 SHIFT 按左键，向左移动一个单词</span></span><br><span class="line">&lt;S+Right&gt;           <span class="comment"># 按住 SHIFT 按右键，向右移动一个单词</span></span><br><span class="line">&lt;S+Up&gt;              <span class="comment"># 按住 SHIFT 按上键，向上翻页</span></span><br><span class="line">&lt;S+Down&gt;            <span class="comment"># 按住 SHIFT 按下键，向下翻页</span></span><br><span class="line">gm                  <span class="comment"># 移动到行中</span></span><br><span class="line">gj                  <span class="comment"># 光标向下移动一个屏幕行（忽略自动换行）</span></span><br><span class="line">gk                  <span class="comment"># 光标向上移动一个屏幕行（忽略自动换行）</span></span><br><span class="line">zz                  <span class="comment"># 调整光标所在行到屏幕中央</span></span><br><span class="line">zt                  <span class="comment"># 调整光标所在行到屏幕上部</span></span><br><span class="line">zb                  <span class="comment"># 调整光标所在行到屏幕下部</span></span><br></pre></td></tr></table></figure><h2 id="插入模式"><a href="#插入模式" class="headerlink" title="插入模式"></a>插入模式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">i                   <span class="comment"># 在光标处进入插入模式</span></span><br><span class="line">I                   <span class="comment"># 在行首进入插入模式</span></span><br><span class="line">a                   <span class="comment"># 在光标后进入插入模式</span></span><br><span class="line">A                   <span class="comment"># 在行尾进入插入模式</span></span><br><span class="line">o                   <span class="comment"># 在下一行插入新行并进入插入模式</span></span><br><span class="line">O                   <span class="comment"># 在上一行插入新行并进入插入模式</span></span><br><span class="line">gi                  <span class="comment"># 进入到上一次插入模式的位置</span></span><br><span class="line">&lt;esc&gt;               <span class="comment"># 退出插入模式</span></span><br><span class="line">Ctrl+[              <span class="comment"># 退出插入模式（等价于 esc 键）</span></span><br></pre></td></tr></table></figure><h2 id="插入模式的命令"><a href="#插入模式的命令" class="headerlink" title="插入模式的命令"></a>插入模式的命令</h2><p><strong>注：由 i, I, a, A, o, O 等命令进入插入模式</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;Up&gt;                <span class="comment"># 光标向上移动</span></span><br><span class="line">&lt;Down&gt;              <span class="comment"># 光标向下移动</span></span><br><span class="line">&lt;Left&gt;              <span class="comment"># 光标向左移动</span></span><br><span class="line">&lt;Right&gt;             <span class="comment"># 光标向右移动</span></span><br><span class="line">&lt;S+Left&gt;            <span class="comment"># 按住 SHIFT 按左键，向左移动一个单词</span></span><br><span class="line">&lt;S+Right&gt;           <span class="comment"># 按住 SHIFT 按右键，向右移动一个单词</span></span><br><span class="line">&lt;S+Up&gt;              <span class="comment"># 按住 SHIFT 按上键，向上翻页</span></span><br><span class="line">&lt;S+Down&gt;            <span class="comment"># 按住 SHIFT 按下键，向下翻页</span></span><br><span class="line">&lt;PageUp&gt;            <span class="comment"># 上翻页</span></span><br><span class="line">&lt;PageDown&gt;          <span class="comment"># 下翻页</span></span><br><span class="line">&lt;Delete&gt;            <span class="comment"># 删除光标处字符</span></span><br><span class="line">&lt;BS&gt;                <span class="comment"># Backspace 向后删除字符</span></span><br><span class="line">&lt;Home&gt;              <span class="comment"># 光标跳转行首</span></span><br><span class="line">&lt;End&gt;               <span class="comment"># 光标跳转行尾</span></span><br><span class="line">Ctrl+d              <span class="comment"># 减少缩进</span></span><br><span class="line">Ctrl+f              <span class="comment"># 减少缩进</span></span><br><span class="line">Ctrl+t              <span class="comment"># 增加缩进</span></span><br><span class="line">Ctrl+h              <span class="comment"># 删除前一个字符，相当于回格键backspace</span></span><br><span class="line">Ctrl+o              <span class="comment"># 临时退出插入模式，执行单条命令又返回插入模式</span></span><br><span class="line">Ctrl+u              <span class="comment"># 当前行删除到行首所有字符</span></span><br><span class="line">Ctrl+w              <span class="comment"># 删除光标前的一个单词</span></span><br><span class="line">Ctrl+\ Ctrl+O       <span class="comment"># 临时退出插入模式（光标保持），执行单条命令又返回插入模式</span></span><br><span class="line">Ctrl+R 0            <span class="comment"># 插入寄存器（内部 0号剪贴板）内容，Ctrl+R 后可跟寄存器名</span></span><br><span class="line">Ctrl+R <span class="string">"            # 插入匿名寄存器内容，相当于插入模式下 p粘贴</span></span><br><span class="line"><span class="string">Ctrl+R =            # 插入表达式计算结果，等号后面跟表达式</span></span><br><span class="line"><span class="string">Ctrl+R :            # 插入上一次命令行命令</span></span><br><span class="line"><span class="string">Ctrl+R /            # 插入上一次搜索的关键字</span></span><br><span class="line"><span class="string">Ctrl+v &#123;char&#125;       # 插入非数字的字面量</span></span><br><span class="line"><span class="string">Ctrl+v &#123;code&#125;       # 插入用三位数字表示的ascii/unicode字符编码，如Ctrl+v 065</span></span><br><span class="line"><span class="string">Ctrl+v 065          # 插入 10进制 ascii 字符（两数字） 065 即 A字符</span></span><br><span class="line"><span class="string">Ctrl+v x41          # 插入 16进制 ascii 字符（三数字） x41 即 A字符</span></span><br><span class="line"><span class="string">Ctrl+v o101         # 插入  8进制 ascii 字符（三数字） o101 即 A字符</span></span><br><span class="line"><span class="string">Ctrl+v u1234        # 插入 16进制 unicode 字符（四数字）</span></span><br><span class="line"><span class="string">Ctrl+v U12345678    # 插入 16进制 unicode 字符（八数字）</span></span><br><span class="line"><span class="string">Ctrl+K &#123;ch1&#125; &#123;ch2&#125;  # 插入 digraph（见 :h digraph），快速输入日文或符号等</span></span><br></pre></td></tr></table></figure><h2 id="自动补全"><a href="#自动补全" class="headerlink" title="自动补全"></a>自动补全</h2><p>在插入模式下，最常用的补全</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+n              <span class="comment"># 插入模式下文字自动补全 </span></span><br><span class="line">Ctrl+P              <span class="comment"># 插入模式下文字自动补全</span></span><br><span class="line">Ctrl+e              <span class="comment"># 有补全列表时，终止这次补全，继续输入</span></span><br></pre></td></tr></table></figure><p><strong>智能补全</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+X              <span class="comment"># 进入补全模式</span></span><br><span class="line">Ctrl+X Ctrl+L       <span class="comment"># 整行补全</span></span><br><span class="line">Ctrl+X Ctrl+N       <span class="comment"># 插入模式下，根据当前文件里关键字补全</span></span><br><span class="line">Ctrl+X Ctrl+K       <span class="comment"># 根据字典补全</span></span><br><span class="line">Ctrl+X Ctrl+T       <span class="comment"># 根据同义词字典补全</span></span><br><span class="line">Ctrl+X Ctrl+F       <span class="comment"># 插入模式下补全文件名</span></span><br><span class="line">Ctrl+X Ctrl+I       <span class="comment"># 根据头文件内关键字补全</span></span><br><span class="line">Ctrl+X Ctrl+]       <span class="comment"># 根据标签补全</span></span><br><span class="line">Ctrl+X Ctrl+D       <span class="comment"># 补全宏定义</span></span><br><span class="line">Ctrl+X Ctrl+V       <span class="comment"># 补全vim命令</span></span><br><span class="line">Ctrl+X Ctrl+U       <span class="comment"># 用户自定义补全方式</span></span><br><span class="line">Ctrl+X Ctrl+S       <span class="comment"># 拼写建议，例如：一个英文单词</span></span><br><span class="line">Ctrl+X Ctrl+O       <span class="comment"># 插入下 Omnifunc 补全</span></span><br></pre></td></tr></table></figure><h2 id="文本编辑"><a href="#文本编辑" class="headerlink" title="文本编辑"></a>文本编辑</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">r                   <span class="comment"># 替换当前字符</span></span><br><span class="line">R                   <span class="comment"># 进入替换模式，直至 ESC 离开</span></span><br><span class="line">s                   <span class="comment"># 替换字符（删除光标处字符，并进入插入模式，前可接数量）</span></span><br><span class="line">S                   <span class="comment"># 替换行（删除当前行，并进入插入模式，前可接数量）</span></span><br><span class="line">cc                  <span class="comment"># 改写当前行（删除当前行并进入插入模式），同 S</span></span><br><span class="line">cw                  <span class="comment"># 改写光标开始处的当前单词</span></span><br><span class="line">ciw                 <span class="comment"># 改写光标所处的单词</span></span><br><span class="line">caw                 <span class="comment"># 改写光标所处的单词，并且包括前后空格（如果有的话）</span></span><br><span class="line">c0                  <span class="comment"># 改写到行首</span></span><br><span class="line">c^                  <span class="comment"># 改写到行首（第一个非零字符）</span></span><br><span class="line">c$                  <span class="comment"># 改写到行末</span></span><br><span class="line">C                   <span class="comment"># 改写到行尾（同c$）</span></span><br><span class="line">ci<span class="string">"                 # 改写双引号中的内容</span></span><br><span class="line"><span class="string">ci'                 # 改写单引号中的内容</span></span><br><span class="line"><span class="string">cib                 # 改写小括号中的内容</span></span><br><span class="line"><span class="string">cab                 # 改写小括号中的内容（包含小括号本身）</span></span><br><span class="line"><span class="string">ci)                 # 改写小括号中的内容</span></span><br><span class="line"><span class="string">ci]                 # 改写中括号中内容</span></span><br><span class="line"><span class="string">ciB                 # 改写大括号中内容</span></span><br><span class="line"><span class="string">caB                 # 改写大括号中的内容（包含大括号本身）</span></span><br><span class="line"><span class="string">ci&#125;                 # 改写大括号中内容</span></span><br><span class="line"><span class="string">cit                 # 改写 xml tag 中的内容</span></span><br><span class="line"><span class="string">cis                 # 改写当前句子</span></span><br><span class="line"><span class="string">c2w                 # 改写下两个单词</span></span><br><span class="line"><span class="string">ct(                 # 改写到小括号前</span></span><br><span class="line"><span class="string">x                   # 删除当前字符，前面可以接数字，3x代表删除三个字符</span></span><br><span class="line"><span class="string">X                   # 向前删除字符</span></span><br><span class="line"><span class="string">dd                  # 删除(剪切)当前行 --&gt; delete (cut) a line </span></span><br><span class="line"><span class="string">d0                  # 删除(剪切)到行首</span></span><br><span class="line"><span class="string">d^                  # 删除(剪切)到行首（第一个非零字符）</span></span><br><span class="line"><span class="string">d$                  # 删除(剪切)到行末</span></span><br><span class="line"><span class="string">D                   # 删除(剪切)到行末（同 d$）</span></span><br><span class="line"><span class="string">dw                  # 删除(剪切)当前单词</span></span><br><span class="line"><span class="string">diw                 # 删除(剪切)光标所处的单词</span></span><br><span class="line"><span class="string">daw                 # 删除(剪切)光标所处的单词，并包含前后空格（如果有的话）</span></span><br><span class="line"><span class="string">di"</span>                 <span class="comment"># 删除双引号中的内容</span></span><br><span class="line">di<span class="string">'                 # 删除单引号中的内容</span></span><br><span class="line"><span class="string">dib                 # 删除小括号中的内容</span></span><br><span class="line"><span class="string">di)                 # 删除小括号中的内容</span></span><br><span class="line"><span class="string">dab                 # 删除小括号内的内容（包含小括号本身）</span></span><br><span class="line"><span class="string">di]                 # 删除中括号中内容</span></span><br><span class="line"><span class="string">diB                 # 删除大括号中内容</span></span><br><span class="line"><span class="string">di&#125;                 # 删除大括号中内容</span></span><br><span class="line"><span class="string">daB                 # 删除大括号内的内容（包含大括号本身）</span></span><br><span class="line"><span class="string">dit                 # 删除 xml tag 中的内容</span></span><br><span class="line"><span class="string">dis                 # 删除当前句子</span></span><br><span class="line"><span class="string">d2w                 # 删除下两个单词</span></span><br><span class="line"><span class="string">dt(                 # 删除到小括号前</span></span><br><span class="line"><span class="string">dgg                 # 删除到文件头部</span></span><br><span class="line"><span class="string">dG                  # 删除到文件尾部</span></span><br><span class="line"><span class="string">d&#125;                  # 删除下一段</span></span><br><span class="line"><span class="string">d&#123;                  # 删除上一段</span></span><br><span class="line"><span class="string">10d                 # 删除当前行开始的10行</span></span><br><span class="line"><span class="string">:10d                # 删除第10行</span></span><br><span class="line"><span class="string">:1,10d              # 删除1-10行</span></span><br><span class="line"><span class="string">J                   # 链接多行为一行</span></span><br><span class="line"><span class="string">.                   # 重复上一次操作</span></span><br><span class="line"><span class="string">~                   # 替换大小写</span></span><br><span class="line"><span class="string">g~iw                # 替换当前单词的大小写</span></span><br><span class="line"><span class="string">gUiw                # 将单词转成大写</span></span><br><span class="line"><span class="string">guiw                # 将当前单词转成小写</span></span><br><span class="line"><span class="string">guu                 # 全行转为小写</span></span><br><span class="line"><span class="string">gUU                 # 全行转为大写</span></span><br><span class="line"><span class="string">&lt;&lt;                  # 减少缩进</span></span><br><span class="line"><span class="string">&gt;&gt;                  # 增加缩进</span></span><br><span class="line"><span class="string">==                  # 自动缩进</span></span><br><span class="line"><span class="string">Ctrl+A              # 增加数字</span></span><br><span class="line"><span class="string">Ctrl+X              # 减少数字</span></span><br></pre></td></tr></table></figure><h2 id="复制粘贴"><a href="#复制粘贴" class="headerlink" title="复制粘贴"></a>复制粘贴</h2><p><strong>copy 命令的格式为</strong>：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:[range]copy&#123;address&#125;</span><br></pre></td></tr></table></figure><p><strong>参数说明</strong>：</p><ul><li>[range] 表示要复制的行范围，其中copy可缩写为 :co 或 :t</li><li>{address} 表示复制的目标位置，这两个参数都可以缺省，用于表示Vim光标所在当前行。</li></ul><p>例如，:5copy. 表示复制Vim当前打开的文件的第 5 行到当前行 (用 . 表示)，即为第 5 行创建一份副本，并放到当前行下方。</p><p>下标列出了使用 copy 命令的缩写形式 t 进行文件复制的一些实例及用途，用于理解复制命令 copy 的用途。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">:3,5t.              <span class="comment"># 把第 3 行到第 5 行的内容复制到当前行下方</span></span><br><span class="line">:t5                 <span class="comment"># 把当前行复制到第 5 行下方</span></span><br><span class="line">:t.                 <span class="comment"># 复制当前行到当前行下方 (等价于普通模式下的 yyp)</span></span><br><span class="line">:t$                 <span class="comment"># 把当前行复制到文本结尾</span></span><br><span class="line">:<span class="string">'&lt;,'</span>&gt;t0            <span class="comment"># 把高亮选中的行复制到文件开头</span></span><br></pre></td></tr></table></figure><p><strong>常用复制粘贴命令</strong>：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">p                   <span class="comment"># 粘贴到光标后</span></span><br><span class="line">P                   <span class="comment"># 粘贴到光标前</span></span><br><span class="line">v                   <span class="comment"># 开始标记</span></span><br><span class="line">y                   <span class="comment"># 复制标记内容</span></span><br><span class="line">V                   <span class="comment"># 开始按行标记</span></span><br><span class="line">Ctrl+V              <span class="comment"># 开始列标记</span></span><br><span class="line">y$                  <span class="comment"># 复制当前位置到本行结束的内容</span></span><br><span class="line">yy                  <span class="comment"># 复制当前行 --&gt; yank (copy) a line</span></span><br><span class="line">Y                   <span class="comment"># 复制当前行，同 yy</span></span><br><span class="line">yiw                 <span class="comment"># 复制当前单词</span></span><br><span class="line">3yy                 <span class="comment"># 复制光标下三行内容</span></span><br><span class="line">v0                  <span class="comment"># 选中当前位置到行首</span></span><br><span class="line">v$                  <span class="comment"># 选中当前位置到行末</span></span><br><span class="line">viw                 <span class="comment"># 选中当前单词</span></span><br><span class="line">vib                 <span class="comment"># 选中小括号内的东西</span></span><br><span class="line">vi)                 <span class="comment"># 选中小括号内的东西</span></span><br><span class="line">vi]                 <span class="comment"># 选中中括号内的东西</span></span><br><span class="line">viB                 <span class="comment"># 选中大括号内的东西</span></span><br><span class="line">vi&#125;                 <span class="comment"># 选中大括号内的东西</span></span><br><span class="line">vis                 <span class="comment"># 选中句子中的东西</span></span><br><span class="line">vab                 <span class="comment"># 选中小括号内的东西（包含小括号本身）</span></span><br><span class="line">va)                 <span class="comment"># 选中小括号内的东西（包含小括号本身）</span></span><br><span class="line">va]                 <span class="comment"># 选中中括号内的东西（包含中括号本身）</span></span><br><span class="line">vaB                 <span class="comment"># 选中大括号内的东西（包含大括号本身）</span></span><br><span class="line">va&#125;                 <span class="comment"># 选中大括号内的东西（包含大括号本身）</span></span><br><span class="line">:<span class="built_in">set</span> paste          <span class="comment"># 允许粘贴模式（避免粘贴时自动缩进影响格式）</span></span><br><span class="line">:<span class="built_in">set</span> nopaste        <span class="comment"># 禁止粘贴模式</span></span><br><span class="line"><span class="string">"?yy                # 复制当前行到寄存器 ? ，问号代表 0-9 的寄存器名称</span></span><br><span class="line"><span class="string">"</span>?d3j               <span class="comment"># 删除光标下三行内容，并放到寄存器 ? ，问号代表 0-9 的寄存器名称</span></span><br><span class="line"><span class="string">"?p                 # 将寄存器 ? 的内容粘贴到光标后</span></span><br><span class="line"><span class="string">"</span>?P                 <span class="comment"># 将寄存器 ? 的内容粘贴到光标前</span></span><br><span class="line">:registers          <span class="comment"># 显示所有寄存器内容</span></span><br><span class="line">:[range]y           <span class="comment"># 复制范围，比如 :20,30y 是复制20到30行，:10y 是复制第十行</span></span><br><span class="line">:[range]d           <span class="comment"># 删除范围，比如 :20,30d 是删除20到30行，:10d 是删除第十行</span></span><br><span class="line">ddp                 <span class="comment"># 交换两行内容：先删除当前行复制到寄存器，并粘贴</span></span><br><span class="line"><span class="string">"_[command]         # 使用[command]删除内容，并且不进行复制（不会污染寄存器）</span></span><br><span class="line"><span class="string">"</span>*[<span class="built_in">command</span>]         <span class="comment"># 使用[command]复制内容到系统剪贴板（需要vim版本有clipboard支持）</span></span><br></pre></td></tr></table></figure><p><strong>文本编辑、复制粘贴中的内容可以简单总结为：</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ci<span class="string">'、ci"、ci(、ci[、ci&#123;、ci&lt;            # 分别更改这些配对标点符号中的文本内容</span></span><br><span class="line"><span class="string">di'</span>、di<span class="string">"、di(或dib、di[、di&#123;或diB、di&lt;   # 分别删除这些配对标点符号中的文本内容</span></span><br><span class="line"><span class="string">yi'、yi"</span>、yi(、yi[、yi&#123;、yi&lt;            <span class="comment"># 分别复制这些配对标点符号中的文本内容</span></span><br><span class="line">vi<span class="string">'、vi"、vi(、vi[、vi&#123;、vi&lt;            # 分别选中这些配对标点符号中的文本内容</span></span><br></pre></td></tr></table></figure><p>cit、dit、yit、vit，分别操作一对标签之间的内容，编辑html、xml很好用！</p><p><strong>另外</strong>，如果把上面的 i 改成 a 可以同时操作配对标点和配对标点内的内容。</p><h2 id="移动文本"><a href="#移动文本" class="headerlink" title="移动文本"></a>移动文本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:[range]move&#123;address&#125;</span><br></pre></td></tr></table></figure><p><strong>参数说明</strong>：</p><ul><li>[range] 表示要移动的行范围</li><li>{address} 表示移动的目标位置，这两个参数都可以缺省</li></ul><p>例如：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:m+1                <span class="comment"># 下移1行</span></span><br><span class="line">:m-2                <span class="comment"># 上移1行</span></span><br><span class="line">:8,10m2             <span class="comment"># 把当前打开文件的第8~10行内容移动到第 2 行下方</span></span><br></pre></td></tr></table></figure></p><h2 id="文本对象"><a href="#文本对象" class="headerlink" title="文本对象"></a>文本对象</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">$                   <span class="comment"># 到行末</span></span><br><span class="line">0                   <span class="comment"># 到行首</span></span><br><span class="line">^                   <span class="comment"># 到行首非空字符</span></span><br><span class="line">iw                  <span class="comment"># 整个单词（不包括分隔符）</span></span><br><span class="line">aw                  <span class="comment"># 整个单词（包括分隔符）</span></span><br><span class="line">iW                  <span class="comment"># 整个 WORD（不包括分隔符）</span></span><br><span class="line">aW                  <span class="comment"># 整个 WORD（包括分隔符）</span></span><br><span class="line">is                  <span class="comment"># 整个句子（不包括分隔符）</span></span><br><span class="line">ib                  <span class="comment"># 小括号内</span></span><br><span class="line">ab                  <span class="comment"># 小括号内（包含小括号本身）</span></span><br><span class="line">iB                  <span class="comment"># 大括号内</span></span><br><span class="line">aB                  <span class="comment"># 大括号内（包含大括号本身）</span></span><br><span class="line">i)                  <span class="comment"># 小括号内</span></span><br><span class="line">a)                  <span class="comment"># 小括号内（包含小括号本身）</span></span><br><span class="line">i]                  <span class="comment"># 中括号内</span></span><br><span class="line">a]                  <span class="comment"># 中括号内（包含中括号本身）</span></span><br><span class="line">i&#125;                  <span class="comment"># 大括号内</span></span><br><span class="line">a&#125;                  <span class="comment"># 大括号内（包含大括号本身）</span></span><br><span class="line">i<span class="string">'                  # 单引号内</span></span><br><span class="line"><span class="string">a'</span>                  <span class="comment"># 单引号内（包含单引号本身）</span></span><br><span class="line">i<span class="string">"                  # 双引号内</span></span><br><span class="line"><span class="string">a"</span>                  <span class="comment"># 双引号内（包含双引号本身）</span></span><br><span class="line">2i)                 <span class="comment"># 往外两层小括号内</span></span><br><span class="line">2a)                 <span class="comment"># 往外两层小括号内（包含小括号本身）</span></span><br><span class="line">2f)                 <span class="comment"># 到第二个小括号处</span></span><br><span class="line">2t)                 <span class="comment"># 到第二个小括号前</span></span><br></pre></td></tr></table></figure><h2 id="撤销与恢复"><a href="#撤销与恢复" class="headerlink" title="撤销与恢复"></a>撤销与恢复</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">u                   <span class="comment"># 撤销命令可以组合，例如Nu N是任意整数，表示撤销N步操作，下同</span></span><br><span class="line">U                   <span class="comment"># 撤销整行操作</span></span><br><span class="line">Ctrl+r              <span class="comment"># 撤销上一次 u 命令</span></span><br><span class="line">Ctrl+R              <span class="comment"># 回退前一个命令</span></span><br></pre></td></tr></table></figure><h2 id="查找替换"><a href="#查找替换" class="headerlink" title="查找替换"></a>查找替换</h2><p><strong>一般模式下的查找命令：</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">/pattern            <span class="comment"># 从光标处向文件尾搜索 pattern</span></span><br><span class="line">?pattern            <span class="comment"># 从光标处向文件头搜索 pattern</span></span><br><span class="line">n                   <span class="comment"># 向同一方向执行上一次搜索</span></span><br><span class="line">N                   <span class="comment"># 向相反方向执行上一次搜索</span></span><br><span class="line">*                   <span class="comment"># 向前搜索光标下的单词</span></span><br><span class="line"><span class="comment">#                   # 向后搜索光标下的单词</span></span><br><span class="line">f&lt;char&gt;             <span class="comment"># 向后搜索当前行第一个为 &lt;char&gt; 的字符，2fv 可以找到第二个v字符</span></span><br><span class="line">F&lt;char&gt;             <span class="comment"># 向前搜索当前行第一个为 &lt;char&gt; 的字符</span></span><br><span class="line">t&lt;char&gt;             <span class="comment"># 向后搜索当前行第一个为 &lt;char&gt; 的字符前</span></span><br><span class="line">T&lt;char&gt;             <span class="comment"># 向前搜索当前行第一个为 &lt;char&gt; 的字符前</span></span><br><span class="line">;                   <span class="comment"># 重复上次的字符查找命令(f/t命令)</span></span><br><span class="line">,                   <span class="comment"># 反转方向查找上次的字符查找命令(f/t命令)</span></span><br><span class="line">tx                  <span class="comment"># 搜索当前行到指定 字符串 之前</span></span><br><span class="line">fx                  <span class="comment"># 搜索当前行到指定 字符串 之处</span></span><br></pre></td></tr></table></figure><p><strong>一般模式下的替换命令：</strong> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:[range]s[ubstitute]/&#123;pattern&#125;/&#123;string&#125;/[flags]</span><br></pre></td></tr></table></figure><p>参数说明：</p><ul><li>pattern：就是要被替换掉的字串，可以用 regexp 來表示</li><li>string：將 pattern 由 string 所取代</li><li>[range]：有以下一些取值：</li></ul><table><thead><tr><th>[range]取值</th><th>含义</th></tr></thead><tbody><tr><td>无</td><td>默认光标所在行</td></tr><tr><td>.</td><td>光标所在当前行</td></tr><tr><td>N</td><td>第 N 行</td></tr><tr><td>$</td><td>最后一行</td></tr><tr><td>‘a</td><td>标记 a 所在的行（之前要用 ma 做过标记）</td></tr><tr><td>$-1</td><td>倒数第二行，可以对某一行加减某个数值获得确定的某行</td></tr><tr><td>1,10</td><td>第1~10行</td></tr><tr><td>1,$</td><td>第一行到最后一行</td></tr><tr><td>1,.</td><td>第一行到当前行</td></tr><tr><td>.,$</td><td>当前行到最后一行</td></tr><tr><td>‘a,’b</td><td>标记 a 所在的行 到 标记 b 所在的行（之前要用 ma、mb 做过标记）</td></tr><tr><td>%</td><td>所有行（和 1,$ 等价）</td></tr><tr><td>?str?</td><td>从当前位置向上搜索，找到第一个str的行（str可以是正则）</td></tr><tr><td>/str/</td><td>从当前位置向下搜索，找到第一个str的行（str可以是正则）</td></tr></tbody></table><p>注意，上面的所有用于range的表示方法都可以通过 +、- 操作来设置相对偏移量。</p><ul><li>[flags]有以下一些取值：</li></ul><table><thead><tr><th>[flags]取值</th><th>含义</th></tr></thead><tbody><tr><td>g</td><td>对指定范围内的所有匹配项（global）进行替换</td></tr><tr><td>c</td><td>在替换前请求用户进行确认（confirm）</td></tr><tr><td>e</td><td>忽略执行过程中的错误</td></tr><tr><td>i</td><td>不区分大小写</td></tr><tr><td>无</td><td>只在指定范围内的第一个匹配项进行替换</td></tr></tbody></table><p>举例：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">:s/p1/p2/g          <span class="comment"># 将当前行中全替换p1为p2</span></span><br><span class="line">:%s/p1/p2/g         <span class="comment"># 将当前文件中全替换p1为p2</span></span><br><span class="line">:%s/p1/p2/gc        <span class="comment"># 将当前文件中全替换p1为p2，并且每处询问你是否替换</span></span><br><span class="line">:10,20s/p1/p2/g     <span class="comment"># 将第10到20行中所有p1替换为p2</span></span><br><span class="line">:%s/1\\2\/3/123/g   <span class="comment"># 将“1\2/3” 替换为 “123”（特殊字符使用反斜杠标注）</span></span><br><span class="line">:%s/\r//g           <span class="comment"># 删除 DOS 换行符 ^M</span></span><br><span class="line">:%s///gn            <span class="comment"># 统计某个模式的匹配个数</span></span><br><span class="line">:%s/^\s*$\n//g      <span class="comment"># 删除Vim打开文件中所有空白行</span></span><br><span class="line">:g/^\s*$/d          <span class="comment"># 删除Vim打开文件中所有空白行</span></span><br><span class="line">:%s/^M$//g          <span class="comment"># 删除Vim文件中显式的 ^M 符号（操作系统换行符问题）</span></span><br></pre></td></tr></table></figure><h2 id="可视模式"><a href="#可视模式" class="headerlink" title="可视模式"></a>可视模式</h2><p><strong>注：Vim 可视模式下可以选择一块编辑区域，然后对选中的文件内容执行插入、删除、替换、改变大小写等操作。！</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">v                   <span class="comment"># 切换到面向字符的可视模式</span></span><br><span class="line">V                   <span class="comment"># 切换到面向行的可视模式</span></span><br><span class="line">Ctrl-V              <span class="comment"># 切换到面向列块的可视模式</span></span><br><span class="line">&gt;                   <span class="comment"># 增加缩进</span></span><br><span class="line">&lt;                   <span class="comment"># 减少缩进</span></span><br><span class="line">d                   <span class="comment"># 删除高亮选中的文字</span></span><br><span class="line">x                   <span class="comment"># 删除高亮选中的文字</span></span><br><span class="line">c                   <span class="comment"># 改写文字，即删除高亮选中的文字并进入插入模式</span></span><br><span class="line">s                   <span class="comment"># 改写文字，即删除高亮选中的文字并进入插入模式</span></span><br><span class="line">y                   <span class="comment"># 拷贝文字</span></span><br><span class="line">~                   <span class="comment"># 转换大小写</span></span><br><span class="line">o                   <span class="comment"># 跳转到标记区的另外一端</span></span><br><span class="line">O                   <span class="comment"># 跳转到标记块的另外一端</span></span><br><span class="line">u                   <span class="comment"># 标记区转换为小写</span></span><br><span class="line">U                   <span class="comment"># 标记区转换为大写</span></span><br><span class="line">gv                  <span class="comment"># 重选上次的高亮选区</span></span><br><span class="line">g Ctrl+G            <span class="comment"># 显示所选择区域的统计信息</span></span><br><span class="line">ggVG                <span class="comment"># 选择全文</span></span><br><span class="line">esc                 <span class="comment"># 按esc键退出可视模式</span></span><br></pre></td></tr></table></figure><p>此外： Vim normal 命令可以在命令行模式执行普通模式下的命令，当 normal 命令与 Vim 可视化模式结合时，只需很少的操作就能完成大量重复性工作。</p><h2 id="注释命令"><a href="#注释命令" class="headerlink" title="注释命令"></a>注释命令</h2><p><strong>多行注释</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+v              <span class="comment"># 进入命令行模式，按Ctrl + v进入可视模式，然后按j, 或者k选中多行，把需要注释的行标记起来</span></span><br><span class="line">I                   <span class="comment"># 按大写字母I，再插入注释符，例如#、//</span></span><br><span class="line">esc                 <span class="comment"># 按esc键就会全部注释了</span></span><br></pre></td></tr></table></figure></p><p><strong>取消多行注释</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+v              <span class="comment"># 进入命令行模式，按Ctrl + v进入可视模式，按字母l横向选中列的个数，例如#、//（需要选中2列）</span></span><br><span class="line">j 或 k              <span class="comment"># 按字母j，或者k选中注释符号</span></span><br><span class="line">d                   <span class="comment"># 按d键就可全部取消注释</span></span><br></pre></td></tr></table></figure><p><strong>复杂注释</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">:起始行号,结束行号s/^/注释符/g（注意冒号）       <span class="comment"># 在指定的行首添加注释</span></span><br><span class="line">:起始行号,结束行号s/^注释符//g（注意冒号）       <span class="comment"># 在指定的行首取消注释</span></span><br><span class="line"></span><br><span class="line">:3,5 s/^/<span class="comment">#/g        # 注释第3-5行</span></span><br><span class="line">:3,5 s/^<span class="comment">#//g        # 解除3-5行的注释</span></span><br><span class="line"></span><br><span class="line">:1,$ s/^/<span class="comment">#/g        # 注释整个文档</span></span><br><span class="line">:1,$ s/^<span class="comment">#//g        # 取消注释整个文档</span></span><br><span class="line"></span><br><span class="line">:%s/^/<span class="comment">#/g           # 注释整个文档，此法更快</span></span><br><span class="line">:%s/^<span class="comment">#//g           # 取消注释整个文档</span></span><br></pre></td></tr></table></figure><h2 id="位置跳转"><a href="#位置跳转" class="headerlink" title="位置跳转"></a>位置跳转</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+O              <span class="comment"># 跳转到上一个位置</span></span><br><span class="line">Ctrl+I              <span class="comment"># 跳转到下一个位置</span></span><br><span class="line">Ctrl+^              <span class="comment"># 跳转到 alternate file (当前窗口的上一个文件）</span></span><br><span class="line">%                   <span class="comment"># 跳转到 &#123;&#125; () [] 的匹配</span></span><br><span class="line">gd                  <span class="comment"># 跳转到局部定义（光标下的单词的定义）</span></span><br><span class="line">gD                  <span class="comment"># 跳转到全局定义（光标下的单词的定义）</span></span><br><span class="line">gf                  <span class="comment"># 打开名称为光标下文件名的文件</span></span><br><span class="line">[[                  <span class="comment"># 跳转到上一个顶层函数（比如C语言以大括号分隔）</span></span><br><span class="line">]]                  <span class="comment"># 跳转到下一个顶层函数（比如C语言以大括号分隔）</span></span><br><span class="line">[m                  <span class="comment"># 跳转到上一个成员函数</span></span><br><span class="line">]m                  <span class="comment"># 跳转到下一个成员函数</span></span><br><span class="line">[&#123;                  <span class="comment"># 跳转到上一处未匹配的 &#123;</span></span><br><span class="line">]&#125;                  <span class="comment"># 跳转到下一处未匹配的 &#125;</span></span><br><span class="line">[(                  <span class="comment"># 跳转到上一处未匹配的 (</span></span><br><span class="line">])                  <span class="comment"># 跳转到下一处未匹配的 )</span></span><br><span class="line">[c                  <span class="comment"># 上一个不同处（diff时）</span></span><br><span class="line">]c                  <span class="comment"># 下一个不同处（diff时）</span></span><br><span class="line">[/                  <span class="comment"># 跳转到 C注释开头</span></span><br><span class="line">]/                  <span class="comment"># 跳转到 C注释结尾</span></span><br><span class="line">``                  <span class="comment"># 回到上次跳转的位置</span></span><br><span class="line"><span class="string">''</span>                  <span class="comment"># 回到上次跳转的位置</span></span><br><span class="line">`.                  <span class="comment"># 回到上次编辑的位置</span></span><br><span class="line"><span class="string">'.                  # 回到上次编辑的位置</span></span><br></pre></td></tr></table></figure><h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">:w                  <span class="comment"># 保存文件（会修改文件的时间戳）</span></span><br><span class="line">:w &lt;filename&gt;       <span class="comment"># 按名称保存文件</span></span><br><span class="line">:x                  <span class="comment"># 保存文件并退出（不会修改文件的时间戳）</span></span><br><span class="line">:e &lt;filename&gt;       <span class="comment"># 打开文件并编辑</span></span><br><span class="line">:saveas &lt;filename&gt;  <span class="comment"># 另存为文件</span></span><br><span class="line">:o &lt;filename&gt;       <span class="comment"># 在当前窗口打开另一个文件</span></span><br><span class="line">:r &lt;filename&gt;       <span class="comment"># 读取文件并将内容插入到光标后</span></span><br><span class="line">:r !dir             <span class="comment"># 将 dir 命令的输出捕获并插入到光标后</span></span><br><span class="line">:only               <span class="comment"># 关闭除光标所在的窗口之外的其他窗口</span></span><br><span class="line">:close              <span class="comment"># 关闭光标所在窗口的文件</span></span><br><span class="line">:q                  <span class="comment"># 关闭光标所在的窗口并退出</span></span><br><span class="line">:q!                 <span class="comment"># 强制退出</span></span><br><span class="line">:qa！               <span class="comment"># 关闭所有窗口(不保存)</span></span><br><span class="line">:qall               <span class="comment"># 放弃所有操作并退出</span></span><br><span class="line">:wa                 <span class="comment"># 保存所有文件</span></span><br><span class="line">:wall               <span class="comment"># 保存所有</span></span><br><span class="line">:wqall              <span class="comment"># 保存所有并退出。</span></span><br><span class="line">:<span class="built_in">cd</span> &lt;path&gt;          <span class="comment"># 切换 Vim 当前路径</span></span><br><span class="line">:<span class="built_in">pwd</span>                <span class="comment"># 显示 Vim 当前路径</span></span><br><span class="line">:new                <span class="comment"># 打开一个新的窗口编辑新文件</span></span><br><span class="line">:enew               <span class="comment"># 在当前窗口创建新文件</span></span><br><span class="line">:vnew               <span class="comment"># 在左右切分的新窗口中编辑新文件</span></span><br><span class="line">:tabnew             <span class="comment"># 在新的标签页中编辑新文件</span></span><br><span class="line">:version            <span class="comment"># 查看Vim版本</span></span><br><span class="line">ZZ                  <span class="comment"># 保存文件（如果有改动的话），并关闭窗口</span></span><br><span class="line">ZQ                  <span class="comment"># 不保存文件关闭窗口</span></span><br></pre></td></tr></table></figure><h2 id="打开文件"><a href="#打开文件" class="headerlink" title="打开文件"></a>打开文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">vim filename        <span class="comment"># 打开或新建文件，并将光标置于第一行首</span></span><br><span class="line">vim + filename      <span class="comment"># 打开文件，并将光标置于最后一行首</span></span><br><span class="line">vim +n filename     <span class="comment"># 打开文件，并将光标置于第 n 行首</span></span><br><span class="line">vim -c cmd file     <span class="comment"># 在打开文件 file 前，先执行指定的Vim命令cmd</span></span><br><span class="line">vim -b file         <span class="comment"># 以二进制模式打开文件，该模式某些特殊字符 (如换行符 ^M) 都可以显示出来</span></span><br><span class="line">vim -d file1 file2  <span class="comment"># 使用Vim同时打开 file1 和 file2 文件并diff两个文件的差异</span></span><br><span class="line">vim -r filename     <span class="comment"># 在上次正用 vim编辑时发生系统崩溃，恢复文件</span></span><br><span class="line">vim -R file         <span class="comment"># 以只读形式打开文件，但是仍然可以使用 :wq! 写入</span></span><br><span class="line">vim -M file         <span class="comment"># 强制性关闭修改功能，无法使用 :wq! 写入 </span></span><br><span class="line">vim -o file1 file2  <span class="comment"># 终端中要打开vim文件时，横向分割显示多个文件</span></span><br><span class="line">vim -O file1 file2  <span class="comment"># 终端中要打开vim文件时，纵向分割显示多个文件</span></span><br><span class="line">vim -x file         <span class="comment"># 以加密方式打开文件</span></span><br><span class="line">vim +/target file   <span class="comment"># 打开 file 并将光标移动到找到的第一个 target 字符串上</span></span><br></pre></td></tr></table></figure><h2 id="已打开文件操作"><a href="#已打开文件操作" class="headerlink" title="已打开文件操作"></a>已打开文件操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">:ls                 <span class="comment"># 查案缓存列表</span></span><br><span class="line">:bn                 <span class="comment"># 切换到下一个缓存</span></span><br><span class="line">:bp                 <span class="comment"># 切换到上一个缓存</span></span><br><span class="line">:bd                 <span class="comment"># 删除缓存</span></span><br><span class="line">:b 1                <span class="comment"># 切换到1号缓存</span></span><br><span class="line">:b abc              <span class="comment"># 切换到文件名为 abc 开头的缓存</span></span><br><span class="line">:badd &lt;filename&gt;    <span class="comment"># 将文件添加到缓存列表</span></span><br><span class="line">:<span class="built_in">set</span> hidden         <span class="comment"># 设置隐藏模式（未保存的缓存可以被切换走，或者关闭）</span></span><br><span class="line">:<span class="built_in">set</span> nohidden       <span class="comment"># 关闭隐藏模式（未保存的缓存不能被切换走，或者关闭）</span></span><br><span class="line">n Ctrl+^            <span class="comment"># 切换缓存，先输入数字的缓存编号，再按 Ctrl + 6</span></span><br></pre></td></tr></table></figure><h2 id="窗口操作"><a href="#窗口操作" class="headerlink" title="窗口操作"></a>窗口操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">:sp &lt;filename&gt;      <span class="comment"># 上下切分窗口并在新窗口打开文件 filename</span></span><br><span class="line">:vs &lt;filename&gt;      <span class="comment"># 左右切分窗口并在新窗口打开文件 filename</span></span><br><span class="line">:split              <span class="comment"># 将当前窗口再复制一个纵向窗口出来，内容同步，游标可以不同</span></span><br><span class="line">:vsplit             <span class="comment"># 将当前窗口再复制一个横向窗口出来，内容同步，游标可以不同</span></span><br><span class="line">Ctrl+W s            <span class="comment"># 上下切分窗口</span></span><br><span class="line">Ctrl+W v            <span class="comment"># 左右切分窗口</span></span><br><span class="line">Ctrl+W w            <span class="comment"># 循环切换到下一个窗口</span></span><br><span class="line">Ctrl+W W            <span class="comment"># 循环切换到上一个窗口</span></span><br><span class="line">Ctrl+W p            <span class="comment"># 跳到上一个访问过的窗口</span></span><br><span class="line">Ctrl+W c            <span class="comment"># 关闭当前窗口</span></span><br><span class="line">Ctrl+W o            <span class="comment"># 关闭其他窗口</span></span><br><span class="line">Ctrl+W h            <span class="comment"># 跳到左边的窗口</span></span><br><span class="line">Ctrl+W j            <span class="comment"># 跳到下边的窗口</span></span><br><span class="line">Ctrl+W k            <span class="comment"># 跳到上边的窗口</span></span><br><span class="line">Ctrl+W l            <span class="comment"># 跳到右边的窗口</span></span><br><span class="line">Ctrl+W +            <span class="comment"># 增加当前窗口的行高，前面可以加数字</span></span><br><span class="line">Ctrl+W -            <span class="comment"># 减少当前窗口的行高，前面可以加数字</span></span><br><span class="line">Ctrl+W &lt;            <span class="comment"># 减少当前窗口的列宽，前面可以加数字</span></span><br><span class="line">Ctrl+W &gt;            <span class="comment"># 增加当前窗口的列宽，前面可以加数字</span></span><br><span class="line">Ctrl+W =            <span class="comment"># 让所有窗口宽高相同</span></span><br><span class="line">Ctrl+W H            <span class="comment"># 将当前窗口移动到最左边</span></span><br><span class="line">Ctrl+W J            <span class="comment"># 将当前窗口移动到最下边</span></span><br><span class="line">Ctrl+W K            <span class="comment"># 将当前窗口移动到最上边</span></span><br><span class="line">Ctrl+W L            <span class="comment"># 将当前窗口移动到最右边</span></span><br><span class="line">Ctrl+W x            <span class="comment"># 交换窗口</span></span><br><span class="line">Ctrl+W f            <span class="comment"># 在新窗口中打开名为光标下文件名的文件</span></span><br><span class="line">Ctrl+W gf           <span class="comment"># 在新标签页中打开名为光标下文件名的文件</span></span><br><span class="line">Ctrl+W R            <span class="comment"># 旋转窗口</span></span><br><span class="line">Ctrl+W T            <span class="comment"># 将当前窗口移到新的标签页中</span></span><br><span class="line">Ctrl+W P            <span class="comment"># 跳转到预览窗口</span></span><br><span class="line">Ctrl+W z            <span class="comment"># 关闭预览窗口</span></span><br><span class="line">Ctrl+W _            <span class="comment"># 纵向最大化当前窗口</span></span><br><span class="line">Ctrl+W |            <span class="comment"># 横向最大化当前窗口</span></span><br></pre></td></tr></table></figure><h2 id="标签页"><a href="#标签页" class="headerlink" title="标签页"></a>标签页</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">:tabs               <span class="comment"># 显示所有标签页</span></span><br><span class="line">:tabe &lt;filename&gt;    <span class="comment"># 在新标签页中打开文件 filename</span></span><br><span class="line">:tabn               <span class="comment"># 下一个标签页</span></span><br><span class="line">:tabp               <span class="comment"># 上一个标签页</span></span><br><span class="line">:tabc               <span class="comment"># 关闭当前标签页</span></span><br><span class="line">:tabo               <span class="comment"># 关闭其他标签页</span></span><br><span class="line">:tabn n             <span class="comment"># 切换到第n个标签页，比如 :tabn 3 切换到第三个标签页</span></span><br><span class="line">:tabm n             <span class="comment"># 标签移动</span></span><br><span class="line">:tabfirst           <span class="comment"># 切换到第一个标签页</span></span><br><span class="line">:tablast            <span class="comment"># 切换到最后一个标签页</span></span><br><span class="line">:tab <span class="built_in">help</span>           <span class="comment"># 在标签页打开帮助</span></span><br><span class="line">:tab drop &lt;file&gt;    <span class="comment"># 如果文件已被其他标签页和窗口打开则跳过去，否则新标签打开</span></span><br><span class="line">:tab split          <span class="comment"># 在新的标签页中打开当前窗口里的文件</span></span><br><span class="line">:tab ball           <span class="comment"># 将缓存中所有文件用标签页打开</span></span><br><span class="line">:<span class="built_in">set</span> showtabline=?  <span class="comment"># 设置为 0 就不显示标签页标签，1会按需显示，2会永久显示</span></span><br><span class="line">ngt                 <span class="comment"># 切换到第n个标签页，比如 2gt 将会切换到第二个标签页</span></span><br><span class="line">gt                  <span class="comment"># 下一个标签页</span></span><br><span class="line">gT                  <span class="comment"># 上一个标签页</span></span><br></pre></td></tr></table></figure><h2 id="书签"><a href="#书签" class="headerlink" title="书签"></a>书签</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">:marks              <span class="comment"># 显示所有书签</span></span><br><span class="line">ma                  <span class="comment"># 保存当前位置到书签 a ，书签名小写字母为文件内，大写全局</span></span><br><span class="line"><span class="string">'a                  # 跳转到书签 a所在的行</span></span><br><span class="line"><span class="string">`a                  # 跳转到书签 a所在位置</span></span><br><span class="line"><span class="string">`.                  # 跳转到上一次编辑的行</span></span><br><span class="line"><span class="string">'</span>A                  <span class="comment"># 跳转到全文书签 A</span></span><br><span class="line">[<span class="string">'                  # 跳转到上一个书签</span></span><br><span class="line"><span class="string">]'</span>                  <span class="comment"># 跳转到下一个书签</span></span><br><span class="line"><span class="string">'&lt;                  # 跳到上次可视模式选择区域的开始</span></span><br><span class="line"><span class="string">'</span>&gt;                  <span class="comment"># 跳到上次可视模式选择区域的结束</span></span><br></pre></td></tr></table></figure><h2 id="帮助信息"><a href="#帮助信息" class="headerlink" title="帮助信息"></a>帮助信息</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">:h tutor            <span class="comment"># 入门文档</span></span><br><span class="line">:h quickref         <span class="comment"># 快速帮助</span></span><br><span class="line">:h index            <span class="comment"># 查询 Vim 所有键盘命令定义</span></span><br><span class="line">:h summary          <span class="comment"># 帮助你更好的使用内置帮助系统</span></span><br><span class="line">:h Ctrl+H           <span class="comment"># 查询普通模式下 Ctrl+H 是干什么的</span></span><br><span class="line">:h i_Ctrl+H         <span class="comment"># 查询插入模式下 Ctrl+H 是干什么的</span></span><br><span class="line">:h i_&lt;Up&gt;           <span class="comment"># 查询插入模式下方向键上是干什么的</span></span><br><span class="line">:h pattern.txt      <span class="comment"># 正则表达式帮助</span></span><br><span class="line">:h <span class="built_in">eval</span>             <span class="comment"># 脚本编写帮助</span></span><br><span class="line">:h <span class="keyword">function</span>-list    <span class="comment"># 查看 VimScript 的函数列表 </span></span><br><span class="line">:h windows.txt      <span class="comment"># 窗口使用帮助</span></span><br><span class="line">:h tabpage.txt      <span class="comment"># 标签页使用帮助</span></span><br><span class="line">:h +timers          <span class="comment"># 显示对 +timers 特性的帮助</span></span><br><span class="line">:h :!               <span class="comment"># 查看如何运行外部命令</span></span><br><span class="line">:h tips             <span class="comment"># 查看 Vim 内置的常用技巧文档</span></span><br><span class="line">:h <span class="built_in">set</span>-termcap      <span class="comment"># 查看如何设置按键扫描码</span></span><br><span class="line">:viusage            <span class="comment"># NORMAL 模式帮助</span></span><br><span class="line">:exusage            <span class="comment"># EX 命令帮助</span></span><br><span class="line">:version            <span class="comment"># 显示当前 Vim 的版本号和特性</span></span><br></pre></td></tr></table></figure><h2 id="外部命令"><a href="#外部命令" class="headerlink" title="外部命令"></a>外部命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">:!<span class="built_in">command</span>           <span class="comment"># 执行一次性shell命令，如下命令：:!pwd</span></span><br><span class="line">:shell              <span class="comment"># 启动一个交互的 shell 执行多个命令，exit命令退出并返回 Vim</span></span><br><span class="line">:!ls                <span class="comment"># 运行外部命令 ls，并等待返回</span></span><br><span class="line">:r !ls              <span class="comment"># 将外部命令 ls 的输出捕获，并插入到光标后</span></span><br><span class="line">:w !sudo tee %      <span class="comment"># sudo以后保存当前文件</span></span><br><span class="line">:call system(<span class="string">'ls'</span>)  <span class="comment"># 调用 ls 命令，但是不显示返回内容</span></span><br><span class="line">:!start notepad     <span class="comment"># Windows 下启动 notepad，最前面可以加 silent</span></span><br><span class="line">:sil !start cmd     <span class="comment"># Windows 下当前目录打开 cmd</span></span><br><span class="line">:%!prog             <span class="comment"># 运行文字过滤程序，如整理 json格式 :%!python -m json.tool</span></span><br></pre></td></tr></table></figure><h2 id="Quickfix-窗口"><a href="#Quickfix-窗口" class="headerlink" title="Quickfix 窗口"></a>Quickfix 窗口</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">:copen              <span class="comment"># 打开 quickfix 窗口（查看编译，grep等信息）</span></span><br><span class="line">:copen 10           <span class="comment"># 打开 quickfix 窗口，并且设置高度为 10</span></span><br><span class="line">:cclose             <span class="comment"># 关闭 quickfix 窗口</span></span><br><span class="line">:cfirst             <span class="comment"># 跳到 quickfix 中第一个错误信息</span></span><br><span class="line">:clast              <span class="comment"># 跳到 quickfix 中最后一条错误信息</span></span><br><span class="line">:cc [nr]            <span class="comment"># 查看错误 [nr]</span></span><br><span class="line">:cnext              <span class="comment"># 跳到 quickfix 中下一个错误信息</span></span><br><span class="line">:cprev              <span class="comment"># 跳到 quickfix 中上一个错误信息</span></span><br></pre></td></tr></table></figure><h2 id="拼写检查"><a href="#拼写检查" class="headerlink" title="拼写检查"></a>拼写检查</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">:<span class="built_in">set</span> spell          <span class="comment"># 打开拼写检查</span></span><br><span class="line">:<span class="built_in">set</span> nospell        <span class="comment"># 关闭拼写检查</span></span><br><span class="line">]s                  <span class="comment"># 下一处错误拼写的单词</span></span><br><span class="line">[s                  <span class="comment"># 上一处错误拼写的单词</span></span><br><span class="line">zg                  <span class="comment"># 加入单词到拼写词表中</span></span><br><span class="line">zug                 <span class="comment"># 撤销上一次加入的单词</span></span><br><span class="line">z=                  <span class="comment"># 拼写建议</span></span><br></pre></td></tr></table></figure><h2 id="代码折叠"><a href="#代码折叠" class="headerlink" title="代码折叠"></a>代码折叠</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">za                  <span class="comment"># 切换折叠</span></span><br><span class="line">zA                  <span class="comment"># 递归切换折叠</span></span><br><span class="line">zc                  <span class="comment"># 折叠光标下代码</span></span><br><span class="line">zC                  <span class="comment"># 折叠光标下所有代码</span></span><br><span class="line">zd                  <span class="comment"># 删除光标下折叠</span></span><br><span class="line">zD                  <span class="comment"># # 递归删除所有折叠</span></span><br><span class="line">zE                  <span class="comment"># 删除所有折叠</span></span><br><span class="line">zf                  <span class="comment"># 创建代码折叠</span></span><br><span class="line">zF                  <span class="comment"># 指定行数创建折叠</span></span><br><span class="line">zi                  <span class="comment"># 切换折叠</span></span><br><span class="line">zm                  <span class="comment"># 所有代码折叠一层</span></span><br><span class="line">zr                  <span class="comment"># 所有代码打开一层</span></span><br><span class="line">zM                  <span class="comment"># 折叠所有代码，设置 foldlevel=0，设置 foldenable</span></span><br><span class="line">zR                  <span class="comment"># 打开所有代码，设置 foldlevel 为最大值</span></span><br><span class="line">zn                  <span class="comment"># 折叠 none，重置 foldenable 并打开所有代码</span></span><br><span class="line">zN                  <span class="comment"># 折叠 normal，重置 foldenable 并恢复所有折叠</span></span><br><span class="line">zo                  <span class="comment"># 打开一层代码</span></span><br><span class="line">zO                  <span class="comment"># 打开光标下所有代码折叠</span></span><br></pre></td></tr></table></figure><h2 id="文档加密解密"><a href="#文档加密解密" class="headerlink" title="文档加密解密"></a>文档加密解密</h2><p><strong>文档加密</strong></p><p>加密方式打开文件时，并在屏幕左下角提示输入密码两次才可进行操作，保存文件退出后必须输入正常密码才能正确打开文件，否则会显示乱码。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vim -x file_name<span class="comment"># 输入加密密码 -&gt; 确认密码! 注意：不修改内容也要保存。:wq，不然密码设定不会生效。</span></span><br><span class="line">:X <span class="comment"># 命令行模式下，输入加密密码 -&gt; 确认密码! 注意：不修改内容也要保存。:wq，不然密码设定不会生效。</span></span><br><span class="line">:<span class="built_in">set</span> key=密码<span class="comment"># 命令行模式下，输入加密密码 -&gt; 确认密码! 注意：不修改内容也要保存。:wq，不然密码设定不会生效。</span></span><br></pre></td></tr></table></figure><p><strong>文档解密</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:X <span class="comment"># 命令行模式下，提示输入密码，不输入而是按 Enter。注意：不修改内容也要保存。:wq，不然解密设定不会生效。</span></span><br><span class="line">:<span class="built_in">set</span> key=<span class="comment"># 命令行模式下，设置key的密码为空。注意：不修改内容也要保存。:wq，不然密码设定不会生效。</span></span><br></pre></td></tr></table></figure><h2 id="宏录制"><a href="#宏录制" class="headerlink" title="宏录制"></a>宏录制</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">qa                  <span class="comment"># 开始录制名字为 a 的宏</span></span><br><span class="line">q                   <span class="comment"># 结束录制宏</span></span><br><span class="line">@a                  <span class="comment"># 播放名字为 a 的宏</span></span><br><span class="line">@@                  <span class="comment"># 播放上一个宏</span></span><br><span class="line">@:                  <span class="comment"># 重复上一个ex命令（即冒号命令）</span></span><br></pre></td></tr></table></figure><h2 id="其他命令"><a href="#其他命令" class="headerlink" title="其他命令"></a>其他命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">Ctrl+X Ctrl+E       <span class="comment"># 插入模式下向上滚屏</span></span><br><span class="line">Ctrl+X Ctrl+Y       <span class="comment"># 插入模式下向下滚屏</span></span><br><span class="line">Ctrl+G              <span class="comment"># 显示正在编辑的文件名，以及大小和位置信息</span></span><br><span class="line">g Ctrl+G            <span class="comment"># 显示文件的：大小，字符数，单词数和行数，可视模式下也可用</span></span><br><span class="line">ga                  <span class="comment"># 显示光标下字符的 ascii 码或者 unicode 编码</span></span><br><span class="line">g8                  <span class="comment"># 显示光标下字符的 utf-8 编码字节序</span></span><br><span class="line">gi                  <span class="comment"># 回到上次进入插入的地方，并切换到插入模式</span></span><br><span class="line">K                   <span class="comment"># 查询光标下单词的帮助</span></span><br><span class="line">Ctrl+PgUp           <span class="comment"># 上个标签页，GVim OK，部分终端软件需设置对应键盘码</span></span><br><span class="line">Ctrl+PgDown         <span class="comment"># 下个标签页，GVim OK，部分终端软件需设置对应键盘码</span></span><br><span class="line">Ctrl+R Ctrl+W       <span class="comment"># 命令模式下插入光标下单词</span></span><br><span class="line">Ctrl+Insert         <span class="comment"># 复制到系统剪贴板（GVIM）</span></span><br><span class="line">Shift+Insert        <span class="comment"># 粘贴系统剪贴板的内容（GVIM）</span></span><br><span class="line">:<span class="built_in">set</span> ff=unix        <span class="comment"># 设置换行为 unix</span></span><br><span class="line">:<span class="built_in">set</span> ff=dos         <span class="comment"># 设置换行为 dos</span></span><br><span class="line">:<span class="built_in">set</span> ff?            <span class="comment"># 查看换行设置</span></span><br><span class="line">:<span class="built_in">set</span> nohl           <span class="comment"># 清除搜索高亮</span></span><br><span class="line">:<span class="built_in">set</span> termcap        <span class="comment"># 查看会从终端接收什么以及会发送给终端什么命令</span></span><br><span class="line">:<span class="built_in">set</span> guicursor=     <span class="comment"># 解决 SecureCRT/PenguiNet 中 NeoVim 局部奇怪字符问题</span></span><br><span class="line">:<span class="built_in">set</span> t_RS= t_SH=    <span class="comment"># 解决 SecureCRT/PenguiNet 中 Vim8.0 终端功能奇怪字符</span></span><br><span class="line">:<span class="built_in">set</span> fo+=a          <span class="comment"># 开启文本段的实时自动格式化</span></span><br><span class="line">:earlier 15m        <span class="comment"># 回退到15分钟前的文件内容</span></span><br><span class="line">:map                <span class="comment"># 来查看当前 Vim 配置的 map 快捷键</span></span><br><span class="line">:inoremap           <span class="comment"># 来查看当前 Vim 配置的 inoremap 快捷键</span></span><br><span class="line">:nnoremap           <span class="comment"># 来查看当前 Vim 配置的 nnoremap 快捷键</span></span><br><span class="line">:.!date             <span class="comment"># 在当前窗口插入时间</span></span><br><span class="line">:%!xxd              <span class="comment"># 开始二进制编辑</span></span><br><span class="line">:%!xxd -r           <span class="comment"># 保存二进制编辑</span></span><br><span class="line">:r !curl -sL &#123;URL&#125;  <span class="comment"># 读取 url 内容添加到光标后</span></span><br><span class="line">:g/^\s*$/d          <span class="comment"># 删除空行</span></span><br><span class="line">:g/green/d          <span class="comment"># 删除所有包含 green 的行</span></span><br><span class="line">:v/green/d          <span class="comment"># 删除所有不包含 green 的行</span></span><br><span class="line">:g/gladiolli/<span class="comment">#      # 搜索单词打印结果，并在结果前加上行号</span></span><br><span class="line">:g/ab.*<span class="built_in">cd</span>.*efg/<span class="comment">#    # 搜索包含 ab,cd 和 efg 的行，打印结果以及行号</span></span><br><span class="line">:v/./,/./-j         <span class="comment"># 压缩空行</span></span><br><span class="line">:Man bash           <span class="comment"># 在 Vim 中查看 man，先调用 :runtime! ftplugin/man.vim 激活</span></span><br><span class="line">/fred\|joe          <span class="comment"># 搜索 fred 或者 joe</span></span><br><span class="line">/\&lt;\d\d\d\d\&gt;       <span class="comment"># 精确搜索四个数字</span></span><br><span class="line">/^\n\&#123;3&#125;            <span class="comment"># 搜索连续三个空行</span></span><br></pre></td></tr></table></figure><h2 id="查看命令历史"><a href="#查看命令历史" class="headerlink" title="查看命令历史"></a>查看命令历史</h2><p>命令行模式下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:<span class="built_in">history</span>                <span class="comment"># 查看所有命令行模式下输入的命令历史</span></span><br><span class="line">:<span class="built_in">history</span> search或 / 或？ <span class="comment"># 查看搜索历史</span></span><br></pre></td></tr></table></figure></p><p>普通模式下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">q/                  <span class="comment"># 查看使用/输入的搜索历史</span></span><br><span class="line">q?                  <span class="comment"># 查看使用？输入的搜索历史</span></span><br><span class="line">q:                  <span class="comment"># 查看命令行历史</span></span><br></pre></td></tr></table></figure></p><h2 id="寄存器"><a href="#寄存器" class="headerlink" title="寄存器"></a>寄存器</h2><p><strong>查看寄存器值</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:reg                   <span class="comment"># 查看所有寄存器值</span></span><br><span class="line">:reg <span class="string">"&#123;register_name&#125;  # 查看指定寄存器值</span></span><br></pre></td></tr></table></figure><p><strong>调取寄存器值</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"&#123;register_name&#125;        # 普通模式下调取寄存器值</span></span><br><span class="line"><span class="string">:Ctrl+r "</span>寄存器名称      <span class="comment"># 命令模式下（输入 Ctrl+r 后Vim会自动打出"寄存器引用符号）</span></span><br><span class="line">Ctrl+r 寄存器名称        <span class="comment"># 插入模式下（无需输入寄存器引用符号"）</span></span><br></pre></td></tr></table></figure><p><strong>Vim寄存器分类</strong></p><table><thead><tr><th>名称</th><th>引用方式</th><th>说明</th></tr></thead><tbody><tr><td>无名寄存器</td><td>“”</td><td>默认寄存器，所有的复制和修改操作（x、s、d、c、y）都会将该数据复制到无名寄存器</td></tr><tr><td>字母寄存器</td><td>“a - “z 或 “A - “Z</td><td>{register_name}只能是一位的26个英文字母，从a-z，A-Z寄存器内容将会合并到对应小写字母内容后边</td></tr><tr><td>复制专用寄存器</td><td>“0（数字0）</td><td>仅当使用复制操作(y)时，该数据将会同时被复制到无名寄存器和复制专用寄存器</td></tr><tr><td>逐级临时缓存寄存器</td><td>“1 - “9</td><td>所有不带范围（‘(’，‘)’，‘{’，‘}’）、操作涉及1行以上的删除修改操作（x、s、d、c）的数据都会复制到逐级临时缓存寄存器，并在新的数据加入时，逐级先后推移。1的数据复制到2，2的复制到3，最后的9寄存器内容将会被删除</td></tr><tr><td>黑洞寄存器</td><td>“_</td><td>几乎所有的操作涉及的数据都会被复制到寄存器，如果想让操作的数据不经过寄存器，可以指定黑洞寄存器，数据到该寄存器就会消失掉，不能显示，也不存在</td></tr><tr><td>系统剪切板</td><td>“+ 或”*</td><td>与Vim外部的GUI交互数据时，需要使用专用的系统剪切板</td></tr><tr><td>表达式寄存器</td><td>“=</td><td>所有寄存器里最特殊的一个，用于计算表达式。输入完该寄存器应用后，会在命令行里提示“=”，按需输入表达式，结果将会显示到光标处</td></tr><tr><td>其他寄存器</td><td></td></tr></tbody></table><h2 id="Vim设置"><a href="#Vim设置" class="headerlink" title="Vim设置"></a>Vim设置</h2><p>编辑Vim配置文件：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:edit <span class="variable">$MYVIMRC</span>      <span class="comment"># 在Vim的命令模式下使用该命令打开 vim 配置文件</span></span><br><span class="line">:<span class="built_in">source</span> <span class="variable">$MYVIMRC</span>    <span class="comment"># Vim 配置文件的改动后，使用该命令为vim配置文件加载新的配置选项，如果vimrc文件恰好是当前活动的缓冲区，则可把此命令简化为:so %。</span></span><br></pre></td></tr></table></figure><p>Vim配置说明：</p><p><strong>注：Vim配置可以在命令模式下单个设置，只在当前窗口生效！</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">syntax              <span class="comment"># 列出已经定义的语法项</span></span><br><span class="line">syntax clear        <span class="comment"># 清除已定义的语法规则</span></span><br><span class="line">syntax on           <span class="comment"># 允许语法高亮</span></span><br><span class="line">syntax off          <span class="comment"># 禁止语法高亮</span></span><br><span class="line"><span class="built_in">set</span> <span class="built_in">history</span>=200     <span class="comment"># 记录 200 条历史命令</span></span><br><span class="line"><span class="built_in">set</span> bs=?            <span class="comment">#  设置BS键模式，现代编辑器为 :set bs=eol,start,indent</span></span><br><span class="line"><span class="built_in">set</span> sw=4            <span class="comment"># 设置缩进宽度为 4</span></span><br><span class="line"><span class="built_in">set</span> ts=4            <span class="comment"># 设置制表符宽度为 4</span></span><br><span class="line"><span class="built_in">set</span> noet            <span class="comment"># 设置不展开 tab 成空格</span></span><br><span class="line"><span class="built_in">set</span> et              <span class="comment"># 设置展开 tab 成空格</span></span><br><span class="line"><span class="built_in">set</span> winaltkeys=no   <span class="comment"># 设置 GVim 下正常捕获 ALT 键</span></span><br><span class="line"><span class="built_in">set</span> nowrap          <span class="comment"># 关闭自动换行</span></span><br><span class="line"><span class="built_in">set</span> ttimeout        <span class="comment"># 允许终端按键检测超时（终端下功能键为一串ESC开头的扫描码）</span></span><br><span class="line"><span class="built_in">set</span> ttm=100         <span class="comment"># 设置终端按键检测超时为100毫秒</span></span><br><span class="line"><span class="built_in">set</span> term=?          <span class="comment"># 设置终端类型，比如常见的 xterm</span></span><br><span class="line"><span class="built_in">set</span> ignorecase      <span class="comment"># 设置搜索是否忽略大小写</span></span><br><span class="line"><span class="built_in">set</span> smartcase       <span class="comment"># 智能大小写，默认忽略大小写，除非搜索内容里包含大写字母</span></span><br><span class="line"><span class="built_in">set</span> list            <span class="comment"># 设置显示制表符和换行符</span></span><br><span class="line"><span class="built_in">set</span> nu              <span class="comment"># 设置显示行号，禁止显示行号可以用 :set nonu</span></span><br><span class="line"><span class="built_in">set</span> number          <span class="comment"># 设置显示行号，禁止显示行号可以用 :set nonumber</span></span><br><span class="line"><span class="built_in">set</span> relativenumber  <span class="comment"># 设置显示相对行号（其他行与当前行的距离）</span></span><br><span class="line"><span class="built_in">set</span> paste           <span class="comment"># 进入粘贴模式（粘贴时禁用缩进等影响格式的东西）</span></span><br><span class="line"><span class="built_in">set</span> nopaste         <span class="comment"># 结束粘贴模式</span></span><br><span class="line"><span class="built_in">set</span> spell           <span class="comment"># 允许拼写检查</span></span><br><span class="line"><span class="built_in">set</span> hlsearch        <span class="comment"># 设置高亮查找</span></span><br><span class="line"><span class="built_in">set</span> ruler           <span class="comment"># 总是显示光标位置</span></span><br><span class="line"><span class="built_in">set</span> nocompatible    <span class="comment"># 设置不兼容原始 vi 模式（必须设置在最开头）</span></span><br><span class="line"><span class="built_in">set</span> incsearch       <span class="comment"># 查找输入时动态增量显示查找结果</span></span><br><span class="line"><span class="built_in">set</span> insertmode      <span class="comment"># Vim 始终处于插入模式下，使用 Ctrl+o 临时执行命令</span></span><br><span class="line"><span class="built_in">set</span> all             <span class="comment"># 列出所有选项设置情况</span></span><br></pre></td></tr></table></figure></p><h2 id="Vim插件"><a href="#Vim插件" class="headerlink" title="Vim插件"></a>Vim插件</h2><h3 id="插件-vim-commentary"><a href="#插件-vim-commentary" class="headerlink" title="插件 - vim-commentary"></a>插件 - <a href="https://github.com/tpope/vim-commentary" target="_blank" rel="noopener">vim-commentary</a></h3><p><strong>vim-commentary</strong>：批量注释工具, 可以注释多行和去除多行注释。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">gcc                 <span class="comment"># 注释当前行</span></span><br><span class="line">gc&#123;motion&#125;          <span class="comment"># 注释 &#123;motion&#125; 所标注的区域，比如 gcap 注释整段</span></span><br><span class="line">gci&#123;                <span class="comment"># 注释大括号内的内容</span></span><br><span class="line">gc                  <span class="comment"># 在 Visual Mode 下面按 gc 注释选中区域</span></span><br><span class="line">:7,17Commentary     <span class="comment"># 注释 7 到 17 行</span></span><br></pre></td></tr></table></figure><h3 id="插件-nerdtree"><a href="#插件-nerdtree" class="headerlink" title="插件 - nerdtree"></a>插件 - <a href="https://github.com/preservim/nerdtree" target="_blank" rel="noopener">nerdtree</a></h3><p><strong>nerdtree</strong>：该插件用于列出当前路径的目录树。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">?                   <span class="comment"># 快速帮助文档</span></span><br><span class="line">o                   <span class="comment"># 打开一个目录或者打开文件，创建的是buffer，也可以用来打开书签</span></span><br><span class="line">go                  <span class="comment"># 打开一个文件，但是光标仍然留在NERDTree，创建的是buffer</span></span><br><span class="line">t                   <span class="comment"># 打开一个文件，创建的是Tab，对书签同样生效</span></span><br><span class="line">T                   <span class="comment"># 打开一个文件，但是光标仍然留在NERDTree，创建的是Tab，对书签同样生效</span></span><br><span class="line">i                   <span class="comment"># 水平分割创建文件的窗口，创建的是buffer</span></span><br><span class="line">gi                  <span class="comment"># 水平分割创建文件的窗口，但是光标仍然留在NERDTree</span></span><br><span class="line">s                   <span class="comment"># 垂直分割创建文件的窗口，创建的是buffer</span></span><br><span class="line">gs                  <span class="comment"># 和gi，go类似</span></span><br><span class="line">x                   <span class="comment"># 收起当前打开的目录</span></span><br><span class="line">X                   <span class="comment"># 收起所有打开的目录</span></span><br><span class="line">e                   <span class="comment"># 以文件管理的方式打开选中的目录</span></span><br><span class="line">D                   <span class="comment"># 删除书签</span></span><br><span class="line">P                   <span class="comment"># 大写，跳转到当前根路径</span></span><br><span class="line">p                   <span class="comment"># 小写，跳转到光标所在的上一级路径</span></span><br><span class="line">K                   <span class="comment"># 跳转到第一个子路径</span></span><br><span class="line">J                   <span class="comment"># 跳转到最后一个子路径</span></span><br><span class="line">Ctrl+j和Ctrl+k      <span class="comment"># 在同级目录和文件间移动，忽略子目录和子文件</span></span><br><span class="line">C                   <span class="comment"># 将根路径设置为光标所在的目录</span></span><br><span class="line">u                   <span class="comment"># 设置上级目录为根路径</span></span><br><span class="line">U                   <span class="comment"># 设置上级目录为跟路径，但是维持原来目录打开的状态</span></span><br><span class="line">r                   <span class="comment"># 刷新光标所在的目录</span></span><br><span class="line">R                   <span class="comment"># 刷新当前根路径</span></span><br><span class="line">I                   <span class="comment"># 显示或者不显示隐藏文件</span></span><br><span class="line">f                   <span class="comment"># 打开和关闭文件过滤器</span></span><br><span class="line">q                   <span class="comment"># 关闭NERDTree</span></span><br><span class="line">A                   <span class="comment"># 全屏显示NERDTree，或者关闭全屏</span></span><br></pre></td></tr></table></figure><h3 id="插件-asyncrun-vim"><a href="#插件-asyncrun-vim" class="headerlink" title="插件 - asyncrun.vim"></a>插件 - <a href="https://github.com/skywind3000/asyncrun.vim" target="_blank" rel="noopener">asyncrun.vim</a></h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:AsyncRun ls        <span class="comment"># 异步运行命令 ls 结果输出到 quickfix 使用 :copen 查看</span></span><br><span class="line">:AsyncRun -raw ls   <span class="comment"># 异步运行命令 ls 结果不匹配 errorformat</span></span><br></pre></td></tr></table></figure><h2 id="Vim模式"><a href="#Vim模式" class="headerlink" title="Vim模式"></a>Vim模式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">普通模式            <span class="comment"># 按 Esc 或 Ctrl+[ 进入，左下角显示文件名或为空</span></span><br><span class="line">插入模式            <span class="comment"># 按i进入，左下角显示 --INSERT--</span></span><br><span class="line">可视模式            <span class="comment"># 按v进入，左下角显示 --VISUAL--</span></span><br><span class="line">替换模式            <span class="comment"># 按r或R开始，左下角显示 --REPLACE--</span></span><br><span class="line">命令行模式          <span class="comment"># 按 : 或者 / 或者 ? 开始</span></span><br></pre></td></tr></table></figure><h2 id="网络资源"><a href="#网络资源" class="headerlink" title="网络资源"></a>网络资源</h2><ul><li>最新版本            <a href="https://github.com/vim/vim" target="_blank" rel="noopener">https://github.com/vim/vim</a>   </li><li>Windows 最新版      <a href="https://github.com/vim/vim-win32-installer/releases" target="_blank" rel="noopener">https://github.com/vim/vim-win32-installer/releases</a></li><li>插件浏览            <a href="http://vimawesome.com" target="_blank" rel="noopener">http://vimawesome.com</a></li><li>正确设置ALT/BS键    <a href="http://www.skywind.me/blog/archives/2021" target="_blank" rel="noopener">http://www.skywind.me/blog/archives/2021</a></li><li>视频教程            <a href="http://vimcasts.org/" target="_blank" rel="noopener">http://vimcasts.org/</a></li><li>中文帮助            <a href="http://vimcdoc.sourceforge.net/doc/help.html" target="_blank" rel="noopener">http://vimcdoc.sourceforge.net/doc/help.html</a></li><li>中文版入门到精通    <a href="https://github.com/wsdjeg/vim-galore-zh_cn" target="_blank" rel="noopener">https://github.com/wsdjeg/vim-galore-zh_cn</a></li><li>五分钟脚本入门      <a href="http://www.skywind.me/blog/archives/2193" target="_blank" rel="noopener">http://www.skywind.me/blog/archives/2193</a></li><li>脚本精通            <a href="http://learnvimscriptthehardway.stevelosh.com/" target="_blank" rel="noopener">http://learnvimscriptthehardway.stevelosh.com/</a></li><li>十六年使用经验      <a href="http://zzapper.co.uk/vimtips.html" target="_blank" rel="noopener">http://zzapper.co.uk/vimtips.html</a></li><li>配色方案            <a href="http://vimcolors.com/" target="_blank" rel="noopener">http://vimcolors.com/</a></li></ul><h2 id="TIPS"><a href="#TIPS" class="headerlink" title="TIPS"></a>TIPS</h2><ul><li>永远不要用 Ctrl+C 代替 <esc> 完全不同的含义，容易错误中断运行的后台脚本</esc></li><li>很多人使用 Ctrl+[ 代替 <esc>，左手小指 Ctrl，右手小指 [ 熟练后很方便</esc></li><li>某些终端中使用 Vim 8 内嵌终端如看到奇怪字符，使用 :set t_RS= t_SH= 解决</li><li>某些终端中使用 NeoVim 如看到奇怪字符，使用 :set guicursor= 解决</li><li>多使用 ciw, ci[, ci”, ci( 以及 diw, di[, di”, di( 命令来快速改写/删除文本</li><li>在行内左右移动光标时，多使用w b e或W B E，而不是h l或方向键，这样会快很多</li><li>Shift 相当于移动加速键， w b e 移动光标很慢，但是 W B E 走的很快</li><li>自己要善于总结新技巧，比如移动到行首非空字符时用 0w 命令比 ^ 命令更容易输入</li><li>在空白行使用 dip 命令可以删除所有临近的空白行，viw 可以选择连续空白</li><li>缩进时使用 &gt;8j &gt;} <ap>ap =i} == 会方便很多</ap></li><li>插入模式下，当你发现一个单词写错了，应该多用 Ctrl+W 这比 <backspace> 快</backspace></li><li>y d c 命令可以很好结合 f t 和 /X 比如 dt) 和 y/end<cr></cr></li><li>c d x 命令会自动填充寄存器 “1 到 “9 , y 命令会自动填充 “0 寄存器</li><li>用 v 命令选择文本时，可以用 o 掉头选择，有时很有用</li><li>写文章时，可以写一段代码块，然后选中后执行 :!python 代码块就会被替换成结果</li><li>搜索后经常使用 :nohl 来消除高亮，使用很频繁，可以 map 到 <backspace> 上</backspace></li><li>搜索时可以用 Ctrl+R Ctrl+W 插入光标下的单词，命令模式也能这么用</li><li>映射按键时，应该默认使用 noremap ，只有特别需要的时候使用 map</li><li>当你觉得做某事很低效时，你应该停下来，然后思考正确的高效方式来完成</li><li>用 y 复制文本后，命令模式中 Ctrl+R 然后按双引号 0 可以插入之前复制内容</li><li>Windows 下的 GVim 可以设置 set rop=type:directx,renmode:5 增强显示</li></ul><h2 id="书籍"><a href="#书籍" class="headerlink" title="书籍"></a>书籍</h2><p><a href="https://pan.baidu.com/s/1tocUYzByGDzKxEqYTFH7ug" target="_blank" rel="noopener">Vim实用技巧</a></p><h2 id="Vim键盘图"><a href="#Vim键盘图" class="headerlink" title="Vim键盘图"></a>Vim键盘图</h2><p><img src="/uploads/vim.png" alt="vim.png"></p><h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul><li><a href="https://github.com/skywind3000/awesome-cheatsheets/blob/master/editors/vim.txt" target="_blank" rel="noopener">https://github.com/skywind3000/awesome-cheatsheets/blob/master/editors/vim.txt</a></li><li><a href="https://github.com/groenewege/vimrc/blob/master/vim_cheat_sheet.txt" target="_blank" rel="noopener">https://github.com/groenewege/vimrc/blob/master/vim_cheat_sheet.txt</a></li><li><a href="http://blog.g-design.net/post/4789778607/vim-cheat-sheet" target="_blank" rel="noopener">http://blog.g-design.net/post/4789778607/vim-cheat-sheet</a></li><li><a href="http://www.keyxl.com/aaa8263/290/VIM-keyboard-shortcuts.htm" target="_blank" rel="noopener">http://www.keyxl.com/aaa8263/290/VIM-keyboard-shortcuts.htm</a></li><li><a href="http://jmcpherson.org/editing.html" target="_blank" rel="noopener">http://jmcpherson.org/editing.html</a></li><li><a href="http://www.fprintf.net/vimCheatSheet.html" target="_blank" rel="noopener">http://www.fprintf.net/vimCheatSheet.html</a></li><li><a href="http://www.ouyaoxiazai.com/article/24/654.html" target="_blank" rel="noopener">http://www.ouyaoxiazai.com/article/24/654.html</a></li><li><a href="http://bbs.it-home.org/thread-80794-1-1.html" target="_blank" rel="noopener">http://bbs.it-home.org/thread-80794-1-1.html</a></li><li><a href="http://www.lpfrx.com/wp-content/uploads/2008/09/vi.jpg" target="_blank" rel="noopener">http://www.lpfrx.com/wp-content/uploads/2008/09/vi.jpg</a></li><li><a href="http://michael.peopleofhonoronly.com/vim/" target="_blank" rel="noopener">http://michael.peopleofhonoronly.com/vim/</a></li><li><a href="https://github.com/hobbestigrou/vimtips-fortune/blob/master/fortunes/vimtips" target="_blank" rel="noopener">https://github.com/hobbestigrou/vimtips-fortune/blob/master/fortunes/vimtips</a></li><li><a href="https://github.com/glts/vim-cottidie/blob/master/autoload/cottidie/tips" target="_blank" rel="noopener">https://github.com/glts/vim-cottidie/blob/master/autoload/cottidie/tips</a></li></ul><hr>]]></content:encoded>
      
      
    </item>
    
    <item>
      <title>Spring文件上传下载</title>
      <link>https://chloneda.github.io//blog/spring-ud/</link>
      <guid>https://chloneda.github.io//blog/spring-ud/</guid>
      <pubDate>Sun, 15 Mar 2020 08:35:47 GMT</pubDate>
      <description>
      
        
        
          &lt;p&gt;&lt;strong&gt;注：本文原链接&lt;/strong&gt;：&lt;a href=&quot;https://www.cnblogs.com/chloneda/p/spring-ud.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://www.cnblogs.c
        
      
      </description>
      
      <content:encoded><![CDATA[<p><strong>注：本文原链接</strong>：<a href="https://www.cnblogs.com/chloneda/p/spring-ud.html" target="_blank" rel="noopener">https://www.cnblogs.com/chloneda/p/spring-ud.html</a></p><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>开发人员多少都会遇到文件的上传下载，特别是Java的Spring框架。那么我们能不能实现一些通用工具类，实现文件上传和下载的功能，可以有效地避免重复开发代码。</p><p>因此，我在网上查找了相关资料，并在此基础上加以改进,支持文件上传及下载功能，而且代码非常精简。如有问题，欢迎大家指正！</p><h1 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h1><p>文件上传及下载需要两个依赖：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-web&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;5.1.6.RELEASE&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!--tomcat中也有servlet-api包，避免冲突添加&lt;scope&gt;provided&lt;/scope&gt;--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;2.5&lt;/version&gt;</span><br><span class="line">    &lt;scope&gt;provided&lt;/scope&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure><p>Spring文件上传一般从请求中获取文件对象，或者直接上传文件获取文件对象。这里提供两个方法，可以满足以上两种情形！当然，如果想自定义文件上传路径，也可以在这两个方法的基础上再增加一个文件路径参数，具体还是看大家的需求情况吧！</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">package com.chloneda.utils;</span><br><span class="line"></span><br><span class="line">import org.springframework.web.multipart.MultipartFile;</span><br><span class="line">import org.springframework.web.multipart.MultipartHttpServletRequest;</span><br><span class="line">import org.springframework.web.multipart.commons.CommonsMultipartResolver;</span><br><span class="line"></span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import java.io.File;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.*;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @author chloneda</span><br><span class="line"> * @description: 文件上传工具类</span><br><span class="line"> */</span><br><span class="line">public class RequestUtils &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取请求中的文件并上传</span><br><span class="line">     *</span><br><span class="line">     * @param request    请求对象</span><br><span class="line">     * @param useNewName 是否使用原文件名称</span><br><span class="line">     */</span><br><span class="line">    public static void uploadFileFromRequest(HttpServletRequest request, boolean useNewName)</span><br><span class="line">            throws IOException &#123;</span><br><span class="line">        CommonsMultipartResolver multipartResolver =</span><br><span class="line">                new CommonsMultipartResolver(request.getSession().getServletContext());</span><br><span class="line">        /** 判断 request 是否有文件上传 */</span><br><span class="line">        <span class="keyword">if</span> (multipartResolver.isMultipart(request)) &#123;</span><br><span class="line">            /** 向上转型获取更多功能 */</span><br><span class="line">            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;</span><br><span class="line">            Iterator&lt;String&gt; iter = multiRequest.getFileNames();</span><br><span class="line">            <span class="keyword">while</span> (iter.hasNext()) &#123;</span><br><span class="line">                MultipartFile multipartFile = multiRequest.getFile(iter.next());</span><br><span class="line">                <span class="keyword">if</span> (multipartFile.isEmpty()) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"文件未上传!"</span>);</span><br><span class="line">                    <span class="built_in">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                uploadFileFromMultipartFile(multipartFile, request, useNewName);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取文件并上传至指定目录</span><br><span class="line">     *</span><br><span class="line">     * @param file       上传的文件</span><br><span class="line">     * @param useNewName 是否使用原文件名称</span><br><span class="line">     * @throws IOException</span><br><span class="line">     */</span><br><span class="line">    public static void uploadFileFromMultipartFile(</span><br><span class="line">            MultipartFile file, HttpServletRequest request, boolean useNewName) throws IOException &#123;</span><br><span class="line">        /** 获取服务器项目发布运行所在地址 */</span><br><span class="line">        String uploadDir = request.getSession().getServletContext().getRealPath(<span class="string">"/"</span>) + <span class="string">"upload"</span>;</span><br><span class="line">        String originalName = file.getOriginalFilename();</span><br><span class="line">        /** 使用时间缀解决文件重名问题 */</span><br><span class="line">        <span class="keyword">if</span> (useNewName) &#123;</span><br><span class="line">            long timestamp = System.currentTimeMillis();</span><br><span class="line">            originalName = timestamp + <span class="string">"-"</span> + originalName;</span><br><span class="line">        &#125;</span><br><span class="line">        String destFilePath = uploadDir + File.separator + originalName;</span><br><span class="line">        file.transferTo(new File(destFilePath));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h1 id="文件下载"><a href="#文件下载" class="headerlink" title="文件下载"></a>文件下载</h1><p>文件下载需要知道文件所在路径，并通过文件流的响应方式返回给前端下载，具体代码如下：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line">package com.chloneda.utils;</span><br><span class="line"></span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line">import java.io.*;</span><br><span class="line">import java.net.URLEncoder;</span><br><span class="line">import java.nio.charset.StandardCharsets;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @author chloneda</span><br><span class="line"> * @description: 返回文件流至浏览器</span><br><span class="line"> */</span><br><span class="line">public class ResponseUtils &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 返回文件流至前端进行展示</span><br><span class="line">     *</span><br><span class="line">     * @param srcFilePath</span><br><span class="line">     * @param contentType 展示contentType一般是image/jpeg、jpg、gif、png等</span><br><span class="line">     * @param response</span><br><span class="line">     */</span><br><span class="line">    public static void responseFileForShow(</span><br><span class="line">            HttpServletResponse response, String srcFilePath, String contentType) &#123;</span><br><span class="line">        File file = checkFile(srcFilePath, <span class="literal">false</span>);</span><br><span class="line">        responseFile(response, file, contentType, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 返回文件流至前端进行下载</span><br><span class="line">     *</span><br><span class="line">     * @param srcFilePath 返回文件的具体路径</span><br><span class="line">     * @param response</span><br><span class="line">     */</span><br><span class="line">    public static void responseFileForLoad(HttpServletResponse response, String srcFilePath) &#123;</span><br><span class="line">        File file = checkFile(srcFilePath, <span class="literal">false</span>);</span><br><span class="line">        responseFile(response, file, <span class="string">"application/octet-stream"</span>, <span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 使用response返回文件流</span><br><span class="line">     *</span><br><span class="line">     * @param response    response进行流的返回</span><br><span class="line">     * @param file        要返回的具体文件</span><br><span class="line">     * @param contentType contentType类型</span><br><span class="line">     * @param isLoad      <span class="literal">true</span>时下载，<span class="literal">false</span>时展示</span><br><span class="line">     */</span><br><span class="line">    private static void responseFile(</span><br><span class="line">            HttpServletResponse response, File file, String contentType, boolean isLoad) &#123;</span><br><span class="line">        try (BufferedInputStream <span class="keyword">in</span> = new BufferedInputStream(new FileInputStream(file));</span><br><span class="line">             BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream())) &#123;</span><br><span class="line">            response.setContentType(contentType);</span><br><span class="line">            /** 如果是下载,指定文件名称 */</span><br><span class="line">            <span class="keyword">if</span> (isLoad) &#123;</span><br><span class="line">                String fileName = file.getName();</span><br><span class="line">                /** 指定字符集解决下载文件名乱码 */</span><br><span class="line">                response.setHeader(<span class="string">"Content-disposition"</span>, <span class="string">"attachment;filename="</span></span><br><span class="line">                        + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()));</span><br><span class="line">            &#125;</span><br><span class="line">            byte[] buffer = new byte[8192];</span><br><span class="line">            int count = 0;</span><br><span class="line">            <span class="keyword">while</span> ((count = in.read(buffer, 0, 8192)) != -1) &#123;</span><br><span class="line">                out.write(buffer, 0, count);</span><br><span class="line">            &#125;</span><br><span class="line">            out.flush();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static File checkFile(String filePath, boolean create) &#123;</span><br><span class="line">        File file = new File(filePath);</span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (create) &#123;</span><br><span class="line">                file.mkdirs();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                throw new IllegalArgumentException(<span class="string">"文件不存在：&#123;&#125;！"</span> + filePath);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">return</span> file;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h1 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h1><p>文件的上传下载是比较普遍的功能需求，这里通过请求响应的方式实现，可以满足日常的开发需求！</p><hr>]]></content:encoded>
      
      
    </item>
    
    <item>
      <title>pom.xml配置文件详解(Maven)</title>
      <link>https://chloneda.github.io//blog/maven-pom/</link>
      <guid>https://chloneda.github.io//blog/maven-pom/</guid>
      <pubDate>Thu, 05 Mar 2020 02:00:27 GMT</pubDate>
      <description>
      
        
        
          &lt;p&gt;&lt;strong&gt;注&lt;/strong&gt;：本文转载自：&lt;a href=&quot;https://blog.csdn.net/u012152619/article/details/51485297&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://blog.
        
      
      </description>
      
      <content:encoded><![CDATA[<p><strong>注</strong>：本文转载自：<a href="https://blog.csdn.net/u012152619/article/details/51485297" target="_blank" rel="noopener">https://blog.csdn.net/u012152619/article/details/51485297</a></p><p><strong>注：博主 Chloneda</strong>：<a href="https://chloneda.github.io/">个人博客</a> | <a href="https://www.cnblogs.com/chloneda" target="_blank" rel="noopener">博客园</a> | <a href="https://github.com/chloneda" target="_blank" rel="noopener">Github</a> | <a href="https://gitee.com/chloneda" target="_blank" rel="noopener">Gitee</a> | <a href="https://www.zhihu.com/people/chl_vip/" target="_blank" rel="noopener">知乎</a></p><p>最近打算对Maven的pom.xml文件进行一下深入研究，做一下总结，惊喜的是网上已经有比较详细的资料，为避免重复造轮子，所以转载了这篇文章。</p><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>setting.xml主要用于配置maven的运行环境等一系列通用的属性，是全局级别的配置文件；而pom.xml主要描述了项目的maven坐标，依赖关系，开发者需要遵循的规则，缺陷管理系统，组织和licenses，以及其他所有的项目相关因素，是项目级别的配置文件。</p><h1 id="基础配置"><a href="#基础配置" class="headerlink" title="基础配置"></a>基础配置</h1><p>一个典型的pom.xml文件配置如下：<br><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span></span><br><span class="line"><span class="xml">xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;</span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 模型版本。maven2.0必须是这样写，现在是maven2唯一支持的版本 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 公司或者组织的唯一标志，并且配置时生成的路径也是由此生成， 如com.winner.trade，maven会将该项目打成的jar包放本地路径：/com/winner/trade --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.winner.trade<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 本项目的唯一ID，一个groupId下面可能多个项目，就是靠artifactId来区分的 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>trade-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 本项目目前所处的版本号 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 打包的机制，如pom,jar, maven-plugin, ejb, war, ear, rar, par，默认为jar --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 帮助定义构件输出的一些附属构件,附属构件与主构件对应，有时候需要加上classifier才能唯一的确定该构件 不能直接定义项目的classifer,因为附属构件不是项目直接默认生成的，而是由附加的插件帮助生成的 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">classifier</span>&gt;</span>...<span class="tag">&lt;/<span class="name">classifier</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 定义本项目的依赖关系 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 每个dependency都对应这一个jar包 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--一般情况下，maven是通过groupId、artifactId、version这三个元素值（俗称坐标）来检索该构件， 然后引入你的工程。如果别人想引用你现在开发的这个项目（前提是已开发完毕并发布到了远程仓库），--&gt;</span> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--就需要在他的pom文件中新建一个dependency节点，将本项目的groupId、artifactId、version写入， maven就会把你上传的jar包下载到他的本地 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.winner.trade<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>trade-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- maven认为，程序对外部的依赖会随着程序的所处阶段和应用场景而变化，所以maven中的依赖关系有作用域(scope)的限制。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--scope包含如下的取值：compile（编译范围）、provided（已提供范围）、runtime（运行时范围）、test（测试范围）、system（系统范围） --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml">            <span class="comment">&lt;!-- 继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析,而是当子项目声明一个依赖（必须描述group ID和 artifact </span></span></span><br><span class="line"><span class="xml">        ID信息），如果group ID和artifact ID以外的一些信息没有描述，则通过group ID和artifact ID 匹配到这里的依赖，并使用这里的依赖信息。 --&gt;</span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span></span><br><span class="line"><span class="xml">                <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml">                    <span class="comment">&lt;!--参见dependencies/dependency元素 --&gt;</span></span></span><br><span class="line"><span class="xml">                    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="xml">                        ......</span></span><br><span class="line"><span class="xml">                    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="xml">                <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span></span><br><span class="line"><span class="xml">    </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 设置指依赖是否可选，默认为false,即子项目默认都继承:为true,则子项目必需显示的引入，与dependencyManagement里定义的依赖类似  --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">optional</span>&gt;</span>false<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 屏蔽依赖关系。 比如项目中使用的libA依赖某个库的1.0版，libB依赖某个库的2.0版，现在想统一使用2.0版，就应该屏蔽掉对1.0版的依赖 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 为pom定义一些常量，在pom中的其它地方可以直接引用 使用方式 如下 ：$</span></span><span class="template-variable">&#123;file.encoding&#125;</span><span class="xml"><span class="comment"> --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">file.encoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">file.encoding</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">java.source.version</span>&gt;</span>1.5<span class="tag">&lt;/<span class="name">java.source.version</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">java.target.version</span>&gt;</span>1.5<span class="tag">&lt;/<span class="name">java.target.version</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml">...</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span></span><br></pre></td></tr></table></figure></p><p>一般来说，上面的几个配置项对任何项目都是必不可少的，定义了项目的基本属性。</p><p>这里有必要对一个不太常用的属性classifier做一下解释，因为有时候引用某个jar包，classifier不写的话会报错。</p><p>classifier元素用来帮助定义构件输出的一些附属构件。附属构件与主构件对应，比如主构件是 kimi-app-2.0.0.jar，该项目可能还会通过使用一些插件生成 如kimi-app-2.0.0-javadoc.jar （Java文档）、 kimi-app-2.0.0-sources.jar（Java源代码） 这样两个附属构件。这时候，javadoc、sources就是这两个附属构件的classifier，这样附属构件也就拥有了自己唯一的坐标。</p><p><strong>classifier的用途</strong></p><ul><li>maven download  javadoc / sources jar包的时候，需要借助classifier指明要下载那个附属构件</li><li>引入依赖的时候，有时候仅凭groupId、artifactId、version无法唯一的确定某个构件，需要借助classifier来进一步明确目标。比如JSON-lib，有时候会同一个版本会提供多个jar包，在JDK1.5环境下是一套，在JDK1.3环境下是一套：</li></ul><p>引用它的时候就要注明JDK版本，否则maven不知道你到底需要哪一套jar包：<br><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;net.sf.json-<span class="class"><span class="keyword">lib</span>&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">    &lt;artifactId&gt;json-<span class="class"><span class="keyword">lib</span>&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">    &lt;version&gt;<span class="number">2.4</span>&lt;<span class="regexp">/version&gt;</span></span><br><span class="line"><span class="regexp">    &lt;classifier&gt;jdk15&lt;/classifier</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/dependency&gt;</span></span><br></pre></td></tr></table></figure></p><h1 id="构建配置"><a href="#构建配置" class="headerlink" title="构建配置"></a>构建配置</h1><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">build</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 产生的构件的文件名，默认值是$</span></span><span class="template-variable">&#123;artifactId&#125;</span><span class="xml"><span class="comment">-$</span></span><span class="template-variable">&#123;version&#125;</span><span class="xml"><span class="comment">。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">finalName</span>&gt;</span>myPorjectName<span class="tag">&lt;/<span class="name">finalName</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 构建产生的所有文件存放的目录,默认为$</span></span><span class="template-variable">&#123;basedir&#125;</span><span class="xml"><span class="comment">/target，即项目根目录下的target --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">directory</span>&gt;</span>$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">/target<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--当项目没有规定目标（Maven2叫做阶段（phase））时的默认值， --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--必须跟命令行上的参数相同例如jar:jar，或者与某个阶段（phase）相同例如install、compile等 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">defaultGoal</span>&gt;</span>install<span class="tag">&lt;/<span class="name">defaultGoal</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--当filtering开关打开时，使用到的过滤器属性文件列表。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目配置信息中诸如$</span></span><span class="template-variable">&#123;spring.version&#125;</span><span class="xml"><span class="comment">之类的占位符会被属性文件中的实际值替换掉 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">filters</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">filter</span>&gt;</span>../filter.properties<span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">filters</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目相关的所有资源路径列表，例如和项目相关的配置文件、属性文件，这些资源被包含在最终的打包文件里。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--描述了资源的目标路径。该路径相对target/classes目录（例如$</span></span><span class="template-variable">&#123;project.build.outputDirectory&#125;</span><span class="xml"><span class="comment">）。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--举个例子，如果你想资源在特定的包里(org.apache.maven.messages)，你就必须该元素设置为org/apache/maven/messages。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--然而，如果你只是想把资源放到源码目录结构里，就不需要该配置。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性，文件在filters元素里列出。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">filtering</span>&gt;</span>true<span class="tag">&lt;/<span class="name">filtering</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--描述存放资源的目录，该路径相对POM路径 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/resources<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--包含的模式列表 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">includes</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.properties<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--排除的模式列表 如果&lt;include&gt;与&lt;exclude&gt;划定的范围存在冲突，以&lt;exclude&gt;为准 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">excludes</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">exclude</span>&gt;</span>jdbc.properties<span class="tag">&lt;/<span class="name">exclude</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">excludes</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--单元测试相关的所有资源路径，配制方法与resources类似 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">testResources</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">testResource</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">targetPath</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">filtering</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">directory</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">includes</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">excludes</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">testResource</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">testResources</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目源码目录，当构建项目的时候，构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">sourceDirectory</span>&gt;</span>$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">\src\main\java<span class="tag">&lt;/<span class="name">sourceDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目脚本源码目录，该目录和源码目录不同， &lt;!-- 绝大多数情况下，该目录下的内容会被拷贝到输出目录(因为脚本是被解释的，而不是被编译的)。 --&gt;</span></span></span><br><span class="line"><span class="xml">         <span class="tag">&lt;<span class="name">scriptSourceDirectory</span>&gt;</span>$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">\src\main\scripts</span></span><br><span class="line"><span class="xml">         <span class="tag">&lt;/<span class="name">scriptSourceDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目单元测试使用的源码目录，当测试项目的时候，构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">testSourceDirectory</span>&gt;</span>$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">\src\test\java<span class="tag">&lt;/<span class="name">testSourceDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--被编译过的应用程序class文件存放的目录。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">outputDirectory</span>&gt;</span>$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">\target\classes<span class="tag">&lt;/<span class="name">outputDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--被编译过的测试class文件存放的目录。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">testOutputDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml">          $</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">\target\test-classes</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">testOutputDirectory</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目的一系列构建扩展,它们是一系列build过程中要使用的产品，会包含在running bulid‘s classpath里面。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--他们可以开启extensions，也可以通过提供条件来激活plugins。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--简单来讲，extensions是在build过程被激活的产品 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">extensions</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--例如，通常情况下，程序开发完成后部署到线上Linux服务器，可能需要经历打包、 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--将包文件传到服务器、SSH连上服务器、敲命令启动程序等一系列繁琐的步骤。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--实际上这些步骤都可以通过Maven的一个插件 wagon-maven-plugin 来自动完成 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--下面的扩展插件wagon-ssh用于通过SSH的方式连接远程服务器， --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--类似的还有支持ftp方式的wagon-ftp插件 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">extension</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.wagon<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>wagon-ssh<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span>&gt;</span>2.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">extension</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">extensions</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--使用的插件列表 。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span><span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-assembly-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5.5<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">executions</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">execution</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--执行目标的标识符，用于标识构建过程中的目标，或者匹配继承过程中需要合并的执行目标 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span>&gt;</span>assembly<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--绑定了目标的构建生命周期阶段，如果省略，目标会被绑定到源数据里配置的默认阶段 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">phase</span>&gt;</span>package<span class="tag">&lt;/<span class="name">phase</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--配置的执行目标 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">goals</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">goal</span>&gt;</span>single<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">goals</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--配置是否被传播到子POM --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">inherited</span>&gt;</span>false<span class="tag">&lt;/<span class="name">inherited</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">execution</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">executions</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--作为DOM对象的配置,配置项因插件而异 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">finalName</span>&gt;</span>$</span><span class="template-variable">&#123;finalName&#125;</span><span class="xml"><span class="tag">&lt;/<span class="name">finalName</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">appendAssemblyId</span>&gt;</span>false<span class="tag">&lt;/<span class="name">appendAssemblyId</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">descriptor</span>&gt;</span>assembly.xml<span class="tag">&lt;/<span class="name">descriptor</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--是否从该插件下载Maven扩展（例如打包和类型处理器）， --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--由于性能原因，只有在真需要下载时，该元素才被设置成true。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">extensions</span>&gt;</span>false<span class="tag">&lt;/<span class="name">extensions</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目引入插件所需要的额外依赖 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>...<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--任何配置是否被传播到子项目 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">inherited</span>&gt;</span>true<span class="tag">&lt;/<span class="name">inherited</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--主要定义插件的共同元素、扩展元素集合，类似于dependencyManagement， --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--所有继承于此项目的子项目都能使用。该插件配置项直到被引用时才会被解析或绑定到生命周期。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--给定插件的任何本地配置都会覆盖这里的配置 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">pluginManagement</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">plugins</span>&gt;</span>...<span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">pluginManagement</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></span></span><br></pre></td></tr></table></figure><p>pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于，pom里的仓库是个性化的。比如一家大公司里的setting文件是公用的，所有项目都用一个setting文件，但各个子项目却会引用不同的第三方库，所以就需要在pom里设置自己需要的仓库地址。</p><h1 id="分发配置"><a href="#分发配置" class="headerlink" title="分发配置"></a>分发配置</h1><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="comment">&lt;!--项目分发信息，在执行mvn deploy后表示要发布的位置。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">distributionManagement</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--部署项目产生的构件到远程仓库需要的信息 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">repository</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--是分配给快照一个唯一的版本号（由时间戳和构建流水号），还是每次都使用相同的版本号 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--参见repositories/repository元素 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">uniqueVersion</span>&gt;</span>true<span class="tag">&lt;/<span class="name">uniqueVersion</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span>&gt;</span> repo-id <span class="tag">&lt;/<span class="name">id</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">name</span>&gt;</span> repo-name<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">url</span>&gt;</span>file://$</span><span class="template-variable">&#123;basedir&#125;</span><span class="xml">/target/deploy <span class="tag">&lt;/<span class="name">url</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">layout</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">repository</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--构件的快照部署到哪里,如果没有配置该元素，默认部署到repository元素配置的仓库 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">snapshotRepository</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">uniqueVersion</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">name</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">url</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">layout</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">snapshotRepository</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--部署项目的网站需要的信息 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">site</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--部署位置的唯一标识符，用来匹配站点和settings.xml文件里的配置 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span>&gt;</span> site-id <span class="tag">&lt;/<span class="name">id</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--部署位置的名称 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">name</span>&gt;</span> site-name<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--部署位置的URL，按protocol://hostname/path形式 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">url</span>&gt;</span>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web <span class="tag">&lt;/<span class="name">url</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">site</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--项目下载页面的URL。如果没有该元素，用户应该参考主页。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--使用该元素的原因是：帮助定位那些不在仓库里的构件（由于license限制）。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">downloadUrl</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--如果构件有了新的groupID和artifact ID（构件移到了新的位置），这里列出构件的重定位信息。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">relocation</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--构件新的group ID --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--构件新的artifact ID --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--构件新的版本号 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--显示给用户的，关于移动的额外信息，例如原因。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">message</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">relocation</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--给出该构件在远程仓库的状态。不得在本地项目中设置该元素，因为这是工具自动更新的。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--有效的值有：none（默认），converted（仓库管理员从Maven 1 POM转换过来）， --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--partner（直接从伙伴Maven 2仓库同步过来），deployed（从Maven 2实例部署），verified（被核实时正确的和最终的）。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">status</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">distributionManagement</span>&gt;</span></span></span><br></pre></td></tr></table></figure><h1 id="仓库配置"><a href="#仓库配置" class="headerlink" title="仓库配置"></a>仓库配置</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--发现依赖和扩展的远程仓库列表。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">repositories</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--包含需要连接到远程仓库的信息 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">repository</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--如何处理远程仓库里发布版本的下载 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">releases</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--true或者false表示该仓库是否为下载某种类型构件（发布版，快照版）开启。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">enabled</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--这里的选项是：always（一直），daily（默认，每日）， --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--interval：X（这里X是以分钟为单位的时间间隔），或者never（从不）。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">updatePolicy</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--当Maven验证构件校验文件失败时该怎么做： --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--ignore（忽略），fail（失败），或者warn（警告）。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">checksumPolicy</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">releases</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置， --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--POM就可以在每个单独的仓库中，为每种类型的构件采取不同的策略。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--例如，可能有人会决定只为开发目的开启对快照版本下载的支持 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">snapshots</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">enabled</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">updatePolicy</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">checksumPolicy</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">snapshots</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--远程仓库唯一标识符。可以用来匹配在settings.xml文件里配置的远程仓库 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">id</span>&gt;</span> repo-id <span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--远程仓库名称 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span> repo-name<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--远程仓库URL，按protocol://hostname/path形式 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span>http://192.168.1.169:9999/repository/ <span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--用于定位和排序构件的仓库布局类型-可以是default（默认）或者legacy（遗留）。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--Maven 2为其仓库提供了一个默认的布局； --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--然而，Maven1.x有一种不同的布局。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--我们可以使用该元素指定布局是default（默认）还是legacy（遗留）。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">layout</span>&gt;</span> default<span class="tag">&lt;/<span class="name">layout</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">repository</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">repositories</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--发现插件的远程仓库列表，这些插件用于构建和报表 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">pluginRepositories</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--包含需要连接到远程插件仓库的信息.参见repositories/repository元素 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">pluginRepository</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">pluginRepositories</span>&gt;</span></span><br></pre></td></tr></table></figure><h1 id="profile配置"><a href="#profile配置" class="headerlink" title="profile配置"></a>profile配置</h1><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="comment">&lt;!--在列的项目构建profile，如果被激活，会修改构建处理 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">profiles</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--根据环境参数或命令行参数激活某个构建处理 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">profile</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">activation</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--profile默认是否激活的标识 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">activeByDefault</span>&gt;</span>false<span class="tag">&lt;/<span class="name">activeByDefault</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--activation有一个内建的java版本检测，如果检测到jdk版本与期待的一样，profile被激活。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">jdk</span>&gt;</span>1.7<span class="tag">&lt;/<span class="name">jdk</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--当匹配的操作系统属性被检测到，profile被激活。os元素可以定义一些操作系统相关的属性。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">os</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的操作系统的名字 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">name</span>&gt;</span>Windows XP<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的操作系统所属家族(如 'windows') --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">family</span>&gt;</span>Windows<span class="tag">&lt;/<span class="name">family</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的操作系统体系结构 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">arch</span>&gt;</span>x86<span class="tag">&lt;/<span class="name">arch</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的操作系统版本 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.2600<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">os</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--如果Maven检测到某一个属性（其值可以在POM中通过$</span></span><span class="template-variable">&#123;名称&#125;</span><span class="xml"><span class="comment">引用），其拥有对应的名称和值，Profile就会被激活。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!-- 如果值字段是空的，那么存在属性名称字段就会激活profile，否则按区分大小写方式匹配属性值字段 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的属性的名称 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">name</span>&gt;</span>mavenVersion<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--激活profile的属性的值 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">value</span>&gt;</span>2.0.3<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--提供一个文件名，通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在，如果不存在则激活profile。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--另一方面，exists则会检查文件是否存在，如果存在则激活profile。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">file</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--如果指定的文件存在，则激活profile。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">exists</span>&gt;</span>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/<span class="tag">&lt;/<span class="name">exists</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--如果指定的文件不存在，则激活profile。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">missing</span>&gt;</span>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/<span class="tag">&lt;/<span class="name">missing</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">file</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">activation</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">build</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">modules</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">repositories</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">pluginRepositories</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependencies</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">reporting</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">dependencyManagement</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">distributionManagement</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">properties</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">profile</span>&gt;</span></span></span><br></pre></td></tr></table></figure><p>profile配置项在setting.xml中也有，是pom.xml中profile元素的裁剪版本，包含了id，activation, repositories, pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为setting.xml只关心构建系统这个整体（这正是settings.xml文件的角色定位），而非单独的项目对象模型设置。如果一个settings中的profile被激活，它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。</p><p>pom.xml中的profile可以看做pom.xml的副本，拥有与pom.xml相同的子元素与配置方法。它包含可选的activation（profile的触发器）和一系列的changes。例如test过程可能会指向不同的数据库（相对最终的deployment）或者不同的dependencies或者不同的repositories，并且是根据不同的JDK来改变的。只需要其中一个成立就可以激活profile，如果第一个条件满足了，那么后面就不会在进行匹配。</p><h1 id="报表配置"><a href="#报表配置" class="headerlink" title="报表配置"></a>报表配置</h1><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="comment">&lt;!--描述使用报表插件产生报表的规范,特定的maven 插件能输出相应的定制和配置报表. --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--当用户执行“mvn site”，这些报表就会运行,在页面导航栏能看到所有报表的链接。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">reporting</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--true，则网站不包括默认的报表。这包括“项目信息”菜单中的报表。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">excludeDefaults</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--所有产生的报表存放到哪里。默认值是$</span></span><span class="template-variable">&#123;project.build.directory&#125;</span><span class="xml"><span class="comment">/site。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">outputDirectory</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--使用的报表插件和他们的配置。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">groupId</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">artifactId</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">version</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">inherited</span> /&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">links</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">link</span>&gt;</span>http://java.sun.com/j2se/1.5.0/docs/api/<span class="tag">&lt;/<span class="name">link</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">links</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--一组报表的多重规范，每个规范可能有不同的配置。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--一个规范（报表集）对应一个执行目标 。例如，有1，2，3，4，5，6，7，8，9个报表。 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--1，2，5构成A报表集，对应一个执行目标。2，5，8构成B报表集，对应另一个执行目标 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">reportSets</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--表示报表的一个集合，以及产生该集合的配置 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">reportSet</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--报表集合的唯一标识符，POM继承时用到 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">id</span>&gt;</span>sunlink<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--产生报表集合时，被使用的报表的配置 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">configuration</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--配置是否被继承到子POMs --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">inherited</span> /&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="comment">&lt;!--这个集合里使用到哪些报表 --&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">reports</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">report</span>&gt;</span>javadoc<span class="tag">&lt;/<span class="name">report</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">reports</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">reportSet</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">reportSets</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span></span><br><span class="line"><span class="xml"> </span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">reporting</span>&gt;</span></span></span><br></pre></td></tr></table></figure><h1 id="环境配置"><a href="#环境配置" class="headerlink" title="环境配置"></a>环境配置</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--项目的问题管理系统(Bugzilla, Jira, Scarab,或任何你喜欢的问题管理系统)的名称和URL，本例为 jira --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">issueManagement</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--问题管理系统（例如jira）的名字， --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">system</span>&gt;</span> jira <span class="tag">&lt;/<span class="name">system</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--该项目使用的问题管理系统的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span> http://jira.clf.com/<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">issueManagement</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目持续集成信息 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ciManagement</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--持续集成系统的名字，例如continuum --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">system</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--该项目使用的持续集成系统的URL（如果持续集成系统有web接口的话）。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--构建完成时，需要通知的开发者/用户的配置项。包括被通知者信息和通知条件（错误，失败，成功，警告） --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">notifiers</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--配置一种方式，当构建中断时，以该方式通知用户/开发者 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">notifier</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--传送通知的途径 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">type</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--发生错误时是否通知 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">sendOnError</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--构建失败时是否通知 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">sendOnFailure</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--构建成功时是否通知 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">sendOnSuccess</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--发生警告时是否通知 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">sendOnWarning</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--不赞成使用。通知发送到哪里 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">address</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--扩展配置项 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">notifier</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">notifiers</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">ciManagement</span>&gt;</span></span><br></pre></td></tr></table></figure><h1 id="项目信息配置"><a href="#项目信息配置" class="headerlink" title="项目信息配置"></a>项目信息配置</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--项目的名称, Maven产生的文档用 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span>banseon-maven <span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目主页的URL, Maven产生的文档用 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span>http://www.clf.com/ <span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目的详细描述, Maven 产生的文档用。 当这个元素能够用HTML格式描述时 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--（例如，CDATA中的文本会被解析器忽略，就可以包含HTML标签），不鼓励使用纯文本描述。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 如果你需要修改产生的web站点的索引页面，你应该修改你自己的索引页文件，而不是调整这里的文档。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">description</span>&gt;</span>A maven project to study maven. <span class="tag">&lt;/<span class="name">description</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--描述了这个项目构建环境中的前提条件。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">prerequisites</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--构建该项目或使用该插件所需要的Maven的最低版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">maven</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">prerequisites</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目创建年份，4位数字。当产生版权信息时需要使用这个值。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">inceptionYear</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目相关邮件列表信息 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mailingLists</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--该元素描述了项目相关的所有邮件列表。自动产生的网站引用这些信息。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mailingList</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--邮件的名称 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span> Demo <span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--发送邮件的地址或链接，如果是邮件地址，创建文档时，mailto: 链接会被自动创建 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">post</span>&gt;</span> clf@126.com<span class="tag">&lt;/<span class="name">post</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--订阅邮件的地址或链接，如果是邮件地址，创建文档时，mailto: 链接会被自动创建 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">subscribe</span>&gt;</span> clf@126.com<span class="tag">&lt;/<span class="name">subscribe</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--取消订阅邮件的地址或链接，如果是邮件地址，创建文档时，mailto: 链接会被自动创建 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">unsubscribe</span>&gt;</span> clf@126.com<span class="tag">&lt;/<span class="name">unsubscribe</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--你可以浏览邮件信息的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">archive</span>&gt;</span> http:/hi.clf.com/<span class="tag">&lt;/<span class="name">archive</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">mailingList</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">mailingLists</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者列表 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">developers</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--某个项目开发者的信息 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">developer</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--SCM里项目开发者的唯一标识符 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">id</span>&gt;</span> HELLO WORLD <span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者的全名 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span> banseon <span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者的email --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">email</span>&gt;</span> banseon@126.com<span class="tag">&lt;/<span class="name">email</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者的主页的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者在项目中扮演的角色，角色元素描述了各种角色 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">roles</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">role</span>&gt;</span> Project Manager<span class="tag">&lt;/<span class="name">role</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">role</span>&gt;</span>Architect <span class="tag">&lt;/<span class="name">role</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">roles</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者所属组织 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">organization</span>&gt;</span> demo<span class="tag">&lt;/<span class="name">organization</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者所属组织的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">organizationUrl</span>&gt;</span>http://hi.clf.com/ <span class="tag">&lt;/<span class="name">organizationUrl</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者属性，如即时消息如何处理等 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dept</span>&gt;</span> No <span class="tag">&lt;/<span class="name">dept</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目开发者所在时区， -11到12范围内的整数。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">timezone</span>&gt;</span> -5<span class="tag">&lt;/<span class="name">timezone</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">developer</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">developers</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目的其他贡献者列表 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">contributors</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目的其他贡献者。参见developers/developer元素 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">contributor</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">email</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">organization</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">organizationUrl</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">roles</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">timezone</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">contributor</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">contributors</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--该元素描述了项目所有License列表。应该只列出该项目的license列表，不要列出依赖项目的license列表。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果列出多个license，用户可以选择它们中的一个而不是接受所有license。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">licenses</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--描述了项目的license，用于生成项目的web站点的license页面，其他一些报表和validation也会用到该元素。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">license</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--license用于法律上的名称 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span> Apache 2 <span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--官方的license正文页面的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span>http://www.clf.com/LICENSE-2.0.txt <span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--项目分发的主要方式： repo，可以从Maven库下载 manual， 用户必须手动下载和安装依赖 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">distribution</span>&gt;</span> repo<span class="tag">&lt;/<span class="name">distribution</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--关于license的补充信息 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">comments</span>&gt;</span> Abusiness-friendly OSS license <span class="tag">&lt;/<span class="name">comments</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">license</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">licenses</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--SCM(Source Control Management)标签允许你配置你的代码库，供Maven web站点和其它插件使用。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">scm</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--SCM的URL,该URL描述了版本库和如何连接到版本库。欲知详情，请看SCMs提供的URL格式和列表。该连接只读。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">connection</span>&gt;</span>scm:svn:http://svn.baidu.com/banseon/maven/<span class="tag">&lt;/<span class="name">connection</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--给开发者使用的，类似connection元素。即该连接不仅仅只读 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">developerConnection</span>&gt;</span>scm:svn:http://svn.baidu.com/banseon/maven/</span><br><span class="line"><span class="tag">&lt;/<span class="name">developerConnection</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--当前代码的标签，在开发阶段默认为HEAD --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">tag</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--指向项目的可浏览SCM库（例如ViewVC或者Fisheye）的URL。 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span> http://svn.baidu.com/banseon<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">scm</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--描述项目所属组织的各种属性。Maven产生的文档用 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">organization</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--组织的全名 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span> demo <span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">&lt;!--组织主页的URL --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">url</span>&gt;</span> http://www.clf.com/<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">organization</span>&gt;</span></span><br></pre></td></tr></table></figure><p><a href="https://www.runoob.com/maven/maven-pom.html" target="_blank" rel="noopener">Maven POM</a></p><hr>]]></content:encoded>
      
      
    </item>
    
    <item>
      <title>Java基础系列-如何优雅地使用close()</title>
      <link>https://chloneda.github.io//blog/java-close/</link>
      <guid>https://chloneda.github.io//blog/java-close/</guid>
      <pubDate>Thu, 05 Mar 2020 01:58:22 GMT</pubDate>
      <description>
      
        
        
          &lt;p&gt;&lt;strong&gt;注：本文出自博主 Chloneda&lt;/strong&gt;：&lt;a href=&quot;https://chloneda.github.io/&quot;&gt;个人博客&lt;/a&gt; | &lt;a href=&quot;https://www.cnblogs.com/chloneda&quot; target=&quot;_b
        
      
      </description>
      
      <content:encoded><![CDATA[<p><strong>注：本文出自博主 Chloneda</strong>：<a href="https://chloneda.github.io/">个人博客</a> | <a href="https://www.cnblogs.com/chloneda" target="_blank" rel="noopener">博客园</a> | <a href="https://github.com/chloneda" target="_blank" rel="noopener">Github</a> | <a href="https://gitee.com/chloneda" target="_blank" rel="noopener">Gitee</a> | <a href="https://www.zhihu.com/people/chl_vip/" target="_blank" rel="noopener">知乎</a></p><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本文尽量采用通俗易懂、循序渐进的方式，让大家真正优雅地使用close()方法！</p><h1 id="问题场景"><a href="#问题场景" class="headerlink" title="问题场景"></a>问题场景</h1><p>平时我们使用资源后一般都会关闭资源，即close()方法，但这个步骤重复性很高，还面临上述执行顺序不明的风险，而且很多人还是不能正确合理地关闭资源。</p><p>我们来看看close()是怎么错误地关闭资源的？</p><h1 id="错误的close"><a href="#错误的close" class="headerlink" title="错误的close()"></a>错误的close()</h1><p>先来看看如下的错误关闭资源方式：</p><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.chloneda.jutils.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @author chloneda</span></span><br><span class="line"><span class="comment"> * @description: close()方法测试</span></span><br><span class="line"><span class="comment"> * 错误的close()</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> CloseTest &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(String[] args) &#123;</span><br><span class="line">        Connection conn = <span class="keyword">null</span>;</span><br><span class="line">        Statement st = <span class="keyword">null</span>;</span><br><span class="line">        ResultSet rs = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//1.加载驱动程序</span></span><br><span class="line">            <span class="keyword">Class</span>.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">            <span class="comment">//2.获得数据库链接</span></span><br><span class="line">            conn = DriverManager.getConnection(<span class="string">"jdbc:mysql://127.0.0.1:3306/common"</span>, <span class="string">"root"</span>, <span class="string">"123456"</span>);</span><br><span class="line">            <span class="comment">//3.通过数据库的连接操作数据库，实现增删改查</span></span><br><span class="line">            st = conn.createStatement();</span><br><span class="line">            rs = st.executeQuery(<span class="string">"select * from new_table"</span>);</span><br><span class="line">            <span class="comment">//4.处理数据库的返回结果</span></span><br><span class="line">            <span class="keyword">while</span> (rs.<span class="keyword">next</span>()) &#123;</span><br><span class="line">                System.out.<span class="keyword">println</span>(rs.getString(<span class="string">"id"</span>) + <span class="string">" "</span> + rs.getString(<span class="string">"name"</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//5.关闭资源</span></span><br><span class="line">            rs.close();</span><br><span class="line">            st.close();</span><br><span class="line">            conn.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>上面代码的资源关闭写在了try代码块中，一旦close方法调用之前(比如<strong>3步骤</strong>)就抛出异常，那么关闭资源的代码就永远不会得到执行。</p><p>如果我们把关闭资源的代码放在finally中行不行呢？<br><figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">try</span> &#123;</span><br><span class="line">    <span class="comment">//1.加载驱动程序</span></span><br><span class="line">    Class.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">    <span class="comment">//2.获得数据库链接</span></span><br><span class="line">    conn = DriverManager.getConnection(<span class="string">"jdbc:mysql://127.0.0.1:3306/common"</span>, <span class="string">"root"</span>, <span class="string">"123456"</span>);</span><br><span class="line">    <span class="comment">//3.通过数据库的连接操作数据库，实现增删改查</span></span><br><span class="line">    st = conn.createStatement();</span><br><span class="line">    rs = st.executeQuery(<span class="string">"select * from new_table"</span>);</span><br><span class="line">    <span class="comment">//4.处理数据库的返回结果</span></span><br><span class="line">    <span class="built_in">while</span> (rs.next()) &#123;</span><br><span class="line">        System.out.<span class="built_in">println</span>(rs.getString(<span class="string">"id"</span>) + <span class="string">" "</span> + rs.getString(<span class="string">"name"</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="built_in">catch</span> (Exception e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125; finally &#123;</span><br><span class="line">    <span class="comment">//5.关闭资源</span></span><br><span class="line">    <span class="built_in">try</span> &#123;</span><br><span class="line">        rs.<span class="built_in">close</span>();</span><br><span class="line">        st.<span class="built_in">close</span>();</span><br><span class="line">        conn.<span class="built_in">close</span>();</span><br><span class="line">    &#125; <span class="built_in">catch</span> (SQLException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p><p>答案是不行！如果在 <strong>2步骤</strong> 的try中conn获得数据库链接抛出异常，那么conn仍然为null，此时进入finally代码块中，执行close()就报空指针异常了，关闭资源没有意义！因此，我们需要在close()之前判断一下conn等是否为空，只有不为空的时候才需要close。</p><h1 id="常见的close"><a href="#常见的close" class="headerlink" title="常见的close()"></a>常见的close()</h1><p>针对上述场景，得到常见的使用close()方式如下：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @author chloneda</span></span><br><span class="line"><span class="comment"> * @description: close()方法测试</span></span><br><span class="line"><span class="comment"> * 常见的close()</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">CloseTest</span> &#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line">        Connection conn = <span class="literal">null</span>;</span><br><span class="line">        Statement st = <span class="literal">null</span>;</span><br><span class="line">        ResultSet rs = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//1.加载驱动程序</span></span><br><span class="line">            Class.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">            <span class="comment">//2.获得数据库链接</span></span><br><span class="line">            conn = DriverManager.getConnection(<span class="string">"jdbc:mysql://127.0.0.1:3306/common"</span>, <span class="string">"root"</span>, <span class="string">"123456"</span>);</span><br><span class="line">            <span class="comment">//3.通过数据库的连接操作数据库，实现增删改查</span></span><br><span class="line">            st = conn.createStatement();</span><br><span class="line">            rs = st.executeQuery(<span class="string">"select * from new_table"</span>);</span><br><span class="line">            <span class="comment">//4.处理数据库的返回结果</span></span><br><span class="line">            <span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">                System.<span class="keyword">out</span>.println(rs.getString(<span class="string">"id"</span>) + <span class="string">" "</span> + rs.getString(<span class="string">"name"</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">//5.关闭资源</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="literal">null</span> != rs) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    rs.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="literal">null</span> != st) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    st.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="literal">null</span> != conn) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    conn.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p><p>这是常见的close()！但是finally代码块的代码重复性太高了，这还只是三个资源的关闭，如果有很多个资源需要在finally中关闭，那不是需要编写很多不优雅的代码？其实，关闭资源是没啥逻辑的代码，我们需要精简代码，减少代码重复性，优雅地编程！</p><h1 id="使用AutoCloseable接口"><a href="#使用AutoCloseable接口" class="headerlink" title="使用AutoCloseable接口"></a>使用AutoCloseable接口</h1><p>自从Java7以后，我们可以使用 <strong>AutoCloseable接口</strong> (Closeable接口也可以)来优雅的关闭资源了 看看修改例子：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @author chloneda</span></span><br><span class="line"><span class="comment"> * @description: close()方法测试</span></span><br><span class="line"><span class="comment"> * 使用AutoCloseable接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">CloseTest</span> &#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line">        Connection conn = <span class="literal">null</span>;</span><br><span class="line">        Statement st = <span class="literal">null</span>;</span><br><span class="line">        ResultSet rs = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//1.加载驱动程序</span></span><br><span class="line">            Class.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">            <span class="comment">//2.获得数据库链接</span></span><br><span class="line">            conn = DriverManager.getConnection(<span class="string">"jdbc:mysql://127.0.0.1:3306/common"</span>, <span class="string">"root"</span>, <span class="string">"123456"</span>);</span><br><span class="line">            <span class="comment">//3.通过数据库的连接操作数据库，实现增删改查</span></span><br><span class="line">            st = conn.createStatement();</span><br><span class="line">            rs = st.executeQuery(<span class="string">"select * from new_table"</span>);</span><br><span class="line">            <span class="comment">//4.处理数据库的返回结果</span></span><br><span class="line">            <span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">                System.<span class="keyword">out</span>.println(rs.getString(<span class="string">"id"</span>) + <span class="string">" "</span> + rs.getString(<span class="string">"name"</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">//5.关闭资源，调用自定义的close()方法</span></span><br><span class="line">            close(rs);</span><br><span class="line">            close(st);</span><br><span class="line">            close(conn);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">close</span>(<span class="params">AutoCloseable closeable</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (closeable != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                closeable.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p><p>上面的finally代码块的代码量是不是减少了许多，就单纯地调用的静态的 <strong>close(AutoCloseable closeable)</strong> 方法。为什么可以这样呢？</p><p>其实Connection、Statement、ResultSet三个接口都继承了<strong>AutoCloseable接口</strong>。所以只要涉及到资源的关闭，继承了AutoCloseable接口，实现了close()方法，我们都可以调用 <strong>close(AutoCloseable closeable)</strong> 方法进行资源关闭。</p><p>此外，java IO流的很多类都实现了 <strong>Closeable接口</strong>，而<strong>Closeable接口</strong>又继承自 <strong>AutoCloseable接口</strong>，也可以调用上面的 <strong>close(AutoCloseable closeable)</strong> 方法进行资源关闭。是不是一语惊醒梦中人啊？</p><h1 id="使用try-with-resources"><a href="#使用try-with-resources" class="headerlink" title="使用try-with-resources"></a>使用try-with-resources</h1><p>其实Java7以后，还有一种关闭资源的方式，也就是 <strong>try-with-resources</strong>，这种方式也是我们推荐的！很优雅！</p><p>我们来看看它是怎么优雅地关闭资源的！<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @author chloneda</span></span><br><span class="line"><span class="comment"> * @description: close()方法测试</span></span><br><span class="line"><span class="comment"> * 使用try-with-resources</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> CloseTest &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(String[] args) <span class="keyword">throws</span> ClassNotFoundException &#123;</span><br><span class="line">        <span class="comment">//1.加载驱动程序</span></span><br><span class="line">        <span class="keyword">Class</span>.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">        <span class="keyword">try</span> (<span class="comment">//2.获得数据库链接</span></span><br><span class="line">             Connection conn = DriverManager.getConnection(<span class="string">"jdbc:mysql://127.0.0.1:3306/common"</span>, <span class="string">"root"</span>, <span class="string">"123456"</span>);</span><br><span class="line">             <span class="comment">//3.通过数据库的连接操作数据库，实现增删改查</span></span><br><span class="line">             Statement st = conn.createStatement();</span><br><span class="line">             ResultSet rs = st.executeQuery(<span class="string">"select * from new_table"</span>)</span><br><span class="line">        ) &#123;</span><br><span class="line">            <span class="comment">//4.处理数据库的返回结果</span></span><br><span class="line">            <span class="keyword">while</span> (rs.<span class="keyword">next</span>()) &#123;</span><br><span class="line">                System.out.<span class="keyword">println</span>(rs.getString(<span class="string">"id"</span>) + <span class="string">" "</span> + rs.getString(<span class="string">"name"</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p><p>这种方式就省略了finally，不必重复编写关闭资源的代码了！而且资源也得到了关闭！怎么验证这个问题？可以查看底下的 <strong>实际应用</strong> 章节！</p><p>其实<strong>try-with-resources</strong>关闭资源的操作，本质上是继承了<strong>java.lang.AutoCloseable接口</strong>，实现了close方法，所以使用<strong>try-with-resources</strong>能关闭资源。很神奇吧！</p><h1 id="实际应用"><a href="#实际应用" class="headerlink" title="实际应用"></a>实际应用</h1><p>这个章节就是验证使用<strong>try-with-resources</strong>可以关闭资源的问题的！</p><p>上面我们说了使用<strong>try-with-resources</strong>关闭资源，只要是继承了<strong>java.lang.AutoCloseable接口</strong> 实现close()方法就可以使用！</p><p>我们自定义一个资源类，来看看实际应用吧！<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.chloneda.jutils.test;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> chloneda</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 资源类, 实现AutoCloseable接口.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Resources</span> <span class="keyword">implements</span> <span class="title">AutoCloseable</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">useResource</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"useResource:&#123;&#125; 正在使用资源!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"close:&#123;&#125; 自动关闭资源!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 使用try-with-resources自动关闭资源测试.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AutoClosableTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/** 使用try-with-resource，自动关闭资源 */</span></span><br><span class="line">        <span class="keyword">try</span> (</span><br><span class="line">                Resources resource = <span class="keyword">new</span> Resources()</span><br><span class="line">        ) &#123;</span><br><span class="line">            resource.useResource();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.getMessage();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"Finally!"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p><p>结果输出。<br><figure class="highlight erlang-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">useResource:&#123;&#125; 正在使用资源!</span><br><span class="line">close:&#123;&#125; 自动关闭资源!</span><br><span class="line">Finally!</span><br></pre></td></tr></table></figure></p><p>看到运行结果了吗？Resources类实现AutoCloseable接口，实现了close()方法，<strong>try-with-resources</strong> 就会自动关闭资源！</p><p>一旦<strong>Resources</strong>类没有继承<strong>java.lang.AutoCloseable接口</strong>，没有实现close()方法，AutoClosableTest类的try模块就在编译期报错，提示信息如下。<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Incompatible types.</span><br><span class="line">Required: java<span class="selector-class">.lang</span><span class="selector-class">.AutoCloseable</span></span><br><span class="line">Found: com<span class="selector-class">.chloneda</span><span class="selector-class">.jutils</span><span class="selector-class">.test</span><span class="selector-class">.Resources</span></span><br></pre></td></tr></table></figure></p><p>最后，需要说明的是<strong>try-with-resources</strong>就是一个<strong>JVM语法糖</strong>！关于JVM语法糖可以查查相关资料，看看Java中有哪些有趣的语法糖！</p><h1 id="尾语"><a href="#尾语" class="headerlink" title="尾语"></a>尾语</h1><p>《Effective Java》在第三版中也推荐使用try-with-resources语句替代try-finally语句。</p><p>所以在处理必须关闭的资源时，使用try-with-resources语句替代try-finally语句。生成的代码更简洁，更清晰，并且生成的异常更有用。 try-with-resources语句在编写必须关闭资源的代码时会更容易，也不会出错，而使用try-finally语句实际上是不可能的。</p><p>如此，推荐大家使用<strong>try-with-resources</strong>优雅地关闭资源！</p><hr>]]></content:encoded>
      
      
    </item>
    
  </channel>
</rss>
